Skip to content

Instantly share code, notes, and snippets.

@lsaffie
Created July 30, 2012 00:27
Show Gist options
  • Save lsaffie/3202888 to your computer and use it in GitHub Desktop.
Save lsaffie/3202888 to your computer and use it in GitHub Desktop.
mail gem usage. Checking mail and reading attachemtns
require 'mail'
def create_observations(file)
#Here's were we create observations based on the file
end
Mail.defaults do
retriever_method :pop3, :address => "pop.gmail.com",
:port => 995,
:user_name => "email@gmail.com",
:password => "*****",
:enable_ssl => true
end
emails = Mail.find(:what => :last, :count => 10, :order => :asc)
emails.each do |mail|
mail.attachments.each do | attachment |
puts mail.from
puts mail.date
if (attachment.content_type.start_with?('text/'))
filename = attachment.filename
begin
file = File.open(filename, "w+b", 0644)
file.write attachment.body.decoded
create_observations(file)
rescue Exception => e
puts "Unable to save data for #{filename} because #{e.message}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment