Skip to content

Instantly share code, notes, and snippets.

@dux
Last active December 10, 2015 23:18
Show Gist options
  • Save dux/4507955 to your computer and use it in GitHub Desktop.
Save dux/4507955 to your computer and use it in GitHub Desktop.
read Gmail email like a boss
# https://github.com/mikel/mail
require 'rubygems'
require 'gmail'
require 'mail'
gmail = Gmail.new('[email protected]', 'pass')
inbox = gmail.mailbox('inbox')
for m in inbox.emails
mail = Mail.read_from_string m.message
p "subject: #{mail.subject}"
p "from: #{mail.from[0]}"
p "to: #{mail.to[0]}"
mail.attachments.each do | attachment |
filename = attachment.filename
begin
File.open('files/' + filename, "w+b", 0644) {|f| f.write attachment.body.decoded}
rescue Exception => e
puts "Unable to save data for #{filename} because #{e.message}"
end
end
exit
end
gmail.logout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment