How to detach mail attachments with Net::IMAP

D

Damjan Rems

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 != nil
# additional attachments attributes
cType = body.parts.media_type
cName = body.parts.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
 
A

a.aboelllef

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 != nil
# additional attachments attributes
cType = body.parts.media_type
cName = body.parts.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


THANKS this helped me alot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top