How to detach mail attachments with Net::IMAP

Discussion in 'Ruby' started by Damjan Rems, Jun 14, 2007.

  1. Damjan Rems

    Damjan Rems Guest

    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
    ---------------------------------

    by
    TheR

    --
    Posted via http://www.ruby-forum.com/.
    Damjan Rems, Jun 14, 2007
    #1
    1. Advertising

  2. Damjan Rems

    Guest

    On Thursday, June 14, 2007 8:49:31 AM UTC+3, Damjan Rems wrote:
    > 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
    > ---------------------------------
    >
    > by
    > TheR
    >
    > --
    > Posted via http://www.ruby-forum.com/.


    THANKS this helped me alot
    , Apr 6, 2013
    #2
    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. codewarrior
    Replies:
    0
    Views:
    526
    codewarrior
    May 4, 2009
  2. jasonnaylor
    Replies:
    1
    Views:
    247
    jasonnaylor
    Apr 16, 2008
  3. Abhishiv Saxena

    Support for IMAP IDLE in net/imap

    Abhishiv Saxena, Jul 3, 2009, in forum: Ruby
    Replies:
    4
    Views:
    190
    Eric Hodel
    Jul 4, 2009
  4. Abhishiv Saxena

    Support for IMAP IDLE in net/imap

    Abhishiv Saxena, Jul 3, 2009, in forum: Ruby
    Replies:
    0
    Views:
    229
    Abhishiv Saxena
    Jul 3, 2009
  5. Don McLaughlin

    modules to detach attachments and form replies?

    Don McLaughlin, Oct 13, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    123
    Don McLaughlin
    Oct 16, 2005
Loading...

Share This Page