Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to detach mail attachments with Net::IMAP

330 views
Skip to first unread message

Damjan Rems

unread,
Jun 14, 2007, 1:49:31 AM6/14/07
to

Since documenattion on this topic is very "non existing" I thought I
would share this with you:

-------------------------------
require 'net/imap'

imap = Net::IMAP.new('my.mail.server')
imap.login('usr', 'pwd')
imap.select('Inbox')
# All msgs in a folder
msgs = imap.search(["SINCE", "1-Jan-1969"])
# Read each message
msgs.each do |msgID|
msg = imap.fetch(msgID, ["ENVELOPE","UID","BODY"] )[0]
# Only those with 'SOMETEXT' in subject are of our interest
if msg.attr["ENVELOPE"].subject.index('SOMETEXT') != nil
body = msg.attr["BODY"]
i = 1
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
File.new(cName,'wb+').write(attachment.unpack('m'))
end
end
end
imap.close
---------------------------------

by
TheR

--
Posted via http://www.ruby-forum.com/.

a.abo...@gmail.com

unread,
Apr 6, 2013, 8:52:32 AM4/6/13
to
THANKS this helped me alot
0 new messages