Tmail + IMAP + Attachments

Discussion in 'Ruby' started by Dustin Webber, Nov 5, 2008.

  1. Dustin Webber, Nov 5, 2008
    #1
    1. Advertising

  2. 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. :)
    --
    Posted via http://www.ruby-forum.com/.
     
    Dustin Webber, Nov 5, 2008
    #2
    1. Advertising

  3. Dustin Webber

    Damjan Rems Guest

    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 != nil
    sType = body.parts.subtype
    cName = body.parts.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


    --
    Posted via http://www.ruby-forum.com/.
     
    Damjan Rems, Nov 5, 2008
    #3
  4. 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 != nil
    > sType = body.parts.subtype
    > cName = body.parts.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
    --
    Posted via http://www.ruby-forum.com/.
     
    Dustin Webber, Nov 6, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henrik Ormåsen

    Curier-IMAP and imap.create()

    Henrik Ormåsen, Aug 19, 2006, in forum: Ruby
    Replies:
    0
    Views:
    178
    Henrik Ormåsen
    Aug 19, 2006
  2. Morten

    TMail attachments

    Morten, Dec 30, 2006, in forum: Ruby
    Replies:
    1
    Views:
    105
    Zouplaz
    Dec 30, 2006
  3. Andrew M. Campbell

    Tmail: Saving Excel attachments to disk.

    Andrew M. Campbell, Feb 5, 2007, in forum: Ruby
    Replies:
    1
    Views:
    183
    Jan Svitok
    Feb 5, 2007
  4. Adam Akhtar
    Replies:
    1
    Views:
    179
    Eric Hodel
    Dec 15, 2008
  5. Adam Akhtar

    how do you add attachments in tmail

    Adam Akhtar, Feb 21, 2009, in forum: Ruby
    Replies:
    2
    Views:
    109
    Reid Thompson
    Feb 22, 2009
Loading...

Share This Page