Forum: Ruby Tmail + IMAP + Attachments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4547f77730a1dcf64d10f842cd1f2042?d=identicon&s=25 Dustin Webber (mephux)
on 2008-11-05 03:38
Attachment: report.rb (2 KB)
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:
4547f77730a1dcf64d10f842cd1f2042?d=identicon&s=25 Dustin Webber (mephux)
on 2008-11-05 03:42
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. :)
Be07c8d0d6867fd9a0d525f7d17600e2?d=identicon&s=25 Damjan Rems (ther)
on 2008-11-05 08:23
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
4547f77730a1dcf64d10f842cd1f2042?d=identicon&s=25 Dustin Webber (mephux)
on 2008-11-06 21:05
Damjan Rems 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
This topic is locked and can not be replied to.