I am working on a Ruby script that will download emails from Gmail and download attachments matching a specific template. I base this on the excellent Mail gem for Ruby. I am using Ruby 1.9.2. I am not so good at Ruby and appreciate any help.
In the code below, emails are an array of emails returned from gmail containing a specific label. What I'm stuck with is a loop through an array of letters and processing what may be multiple attachments for each letter. The inner loop of email messages [index] .attachments.each works, if I specify an index value, I could not complete the first loop to see all the index values โโof the array.
emails = Mail.find(:order => :asc, :mailbox => 'label')
emails.each_with_index do |index|
emails[index].attachments.each do | attachment |
if (attachment.filename.start_with?('attachment'))
filename = attachment.filename
begin
File.open(file_dir + 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
end
end