Skip to content

Instantly share code, notes, and snippets.

@acidprime
Created December 3, 2014 07:26
Show Gist options
  • Save acidprime/46ab906117b317a40dc9 to your computer and use it in GitHub Desktop.
Save acidprime/46ab906117b317a40dc9 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'net/imap'
imap = Net::IMAP.new('imap.gmail.com', 993, true)
imap.login('[email protected]', 'wouldntyouliketoknow')
imap.select('Inbox')
# All msgs in a folder
msgs = imap.search(["SUBJECT", "Motion"])
# Read each message
msgs.each do |msgID|
puts "Processing Message: #{msgID}"
msg = imap.fetch(msgID, ["ENVELOPE","UID","BODY"] )[0]
body = msg.attr["BODY"]
i = 0
while body.parts[i] != nil
# additional attachments attributes
cType = body.parts[i].media_type
cName = body.parts[i].param['NAME']
i+=1
# fetch attachment.
attachment = imap.fetch(msgID, "BODY[#{i}]")[0].attr["BODY[#{i}]"]
# Save message, BASE64 decoded
puts "Saving attachement: #{cName}"
File.new("#{msgID}_#{cName}",'wb+').write(attachment.unpack('m'))
end
end
imap.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment