Created
October 4, 2013 04:03
-
-
Save mikedemers/6820794 to your computer and use it in GitHub Desktop.
Archive all email in a google mail account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'gmail' | |
require 'fileutils' | |
username = '' | |
password = '' | |
root = Dir.pwd | |
Gmail.connect(username, password) do |gmail| | |
begin | |
gmail.mailbox("[Gmail]/All Mail").emails.each_with_index do |email, n| | |
begin | |
path = email.message.date.strftime('%Y/%m/%d') | |
filename = '%s_%06d' % [ email.message.date.strftime('%H%M%S'), (email.uid || rand(999999)) ] | |
#STDOUT.puts("[%6d]: #{path}/#{filename}" % n) | |
FileUtils.mkpath("#{root}/#{path}") | |
File.open("#{root}/#{path}/#{filename}", "w") do |io| | |
io.write(email.message.raw_source) | |
end | |
rescue Exception => e | |
put "Message #{email.uid} threw #{e.class}: #{e.message}" | |
end | |
end | |
rescue Exception => e | |
puts "Caught #{e.class}: #{e.message}" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment