Created
July 30, 2012 00:27
-
-
Save lsaffie/3202888 to your computer and use it in GitHub Desktop.
mail gem usage. Checking mail and reading attachemtns
This file contains hidden or 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
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 protected]", | |
: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