Skip to content

Instantly share code, notes, and snippets.

@mikedemers
Created October 4, 2013 04:03
Show Gist options
  • Save mikedemers/6820794 to your computer and use it in GitHub Desktop.
Save mikedemers/6820794 to your computer and use it in GitHub Desktop.
Archive all email in a google mail account
#!/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