# Tmail + IMAP + Attachments

**URL:** <https://www.ruby-forum.com/t/tmail-imap-attachments/152552>\
**Category:** Ruby\
**Created:** [November 5, 2008, 3:38am UTC](https://www.ruby-forum.com/t/tmail-imap-attachments/152552 "2008-11-05T03:38:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [November 5, 2008, 3:38am UTC](https://www.ruby-forum.com/t/tmail-imap-attachments/152552/1 "2008-11-05T03:38:46Z")

</div>

I am working on a script to log in to a mail server via IMAP and pull  
tables of content from certain emails. All the tables are accompanied by  
graphs which are quite nice.

How can I pull these attachments with Tmail, is this possible?

Here is what I have so far:

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [November 5, 2008, 3:42am UTC](https://www.ruby-forum.com/t/tmail-imap-attachments/152552/2 "2008-11-05T03:42:31Z")

</div>

Dustin Webber wrote:

> I am working on a script to log in to a mail server via IMAP and pull  
> tables of content from certain emails. All the tables are accompanied by  
> graphs which are quite nice.
> 
> How can I pull these attachments with Tmail, is this possible?
> 
> Here is what I have so far:

Sorry, forgot some important information.

Filetype: image/gif #=\> Image format of the graphs I want to save.

I appreciate any help I can get. 🙂

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [November 5, 2008, 8:23am UTC](https://www.ruby-forum.com/t/tmail-imap-attachments/152552/3 "2008-11-05T08:23:16Z")

</div>

Didn’t tried with tmail but this is how it goes with IMAP.

imap = Net::IMAP.new(‘my.mail.server’)  
imap.login(‘usr’, ‘pwd’)  
imap.select(‘Inbox’)

# all msgs

n = imap.search([“SINCE”, “1-Jan-1969”])  
n.each do |msgID|  
msg = imap.fetch(msgID, [“ENVELOPE”,“UID”,“BODY”] )[0]  
unless msg.attr[“ENVELOPE”].subject.index(‘SOME STRING’).nil?  
body = msg.attr[“BODY”]  
i = 1  
while body.parts[i] != nil  
sType = body.parts[i].subtype  
cName = body.parts[i].param[‘NAME’]  
i+=1  
attachment = imap.fetch(msgID,  
“BODY[#{i}]”)[0].attr[“BODY[#{i}]”]  
File.open($wDir+cName,‘wb+’) { |f|  
f.write(attachment.unpack(‘m’)) } unless attachment.nil?  
end  
end  
end

by  
TheR

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [November 6, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/tmail-imap-attachments/152552/4 "2008-11-06T21:05:22Z")

</div>

Damjan R. wrote:

> Didn’t tried with tmail but this is how it goes with IMAP.
> 
> imap = Net::IMAP.new(‘my.mail.server’)  
> imap.login(‘usr’, ‘pwd’)  
> imap.select(‘Inbox’)
> 
> # all msgs
> 
> n = imap.search([“SINCE”, “1-Jan-1969”])  
> n.each do |msgID|  
> msg = imap.fetch(msgID, [“ENVELOPE”,“UID”,“BODY”] )[0]  
> unless msg.attr[“ENVELOPE”].subject.index(‘SOME STRING’).nil?  
> body = msg.attr[“BODY”]  
> i = 1  
> while body.parts[i] != nil  
> sType = body.parts[i].subtype  
> cName = body.parts[i].param[‘NAME’]  
> i+=1  
> attachment = imap.fetch(msgID,  
> “BODY[#{i}]”)[0].attr[“BODY[#{i}]”]  
> File.open($wDir+cName,‘wb+’) { |f|  
> f.write(attachment.unpack(‘m’)) } unless attachment.nil?  
> end  
> end  
> end
> 
> by  
> TheR

Thank you that worked. Still having some problems getting it to work but  
I am sure it will come to me.

Thank you again!

- Dustin
