How to strip the attachment from an IMAP mail

While trying hard long and long hours to write a perl script that can extract attachments from pieces of e-mail, I realized that there were no clear info on the web on how to do, and that if I successed, I would publish that script.

Finally I succeded. That is actually very easy and that is probably why nothing was ever published about it. But, when you are a beginner in Perl (I learned it in 1 week), you normally need a little bit more help. Also, I was not alone in that case and there were a numerous questions on various forum, but without satisfying answers.

As I said, my script is extrimely simple.

It is based on the Mail::IMAPTalk module to establish the IMAP connection and on the MIME::Parser module to parse the mail as a MIME object (given by the first module).

I know there is a dedicated CPAN module to strip attachment : Mail::Attachment::Stripper, but, I could never get it to work and the author did not answer to my e-mail.

You will find the script here with some comments that should explain well :

attachment-stripper.zip

Of course, that is just a quick and dirty base : it is up to you to improve / correct / extend it to fit your needs. If so, be nice to update me here. ;)

Comments 14

  1. Patrick wrote:

    Thank you for this!
    I am trying to accomplish this too and it is indeed hard to find anything to get you on the way ;)

    Posted 09 Apr 2008 at 12:17 pm
  2. octopus wrote:

    Its really great. I have just one question. If there is SSL authentication required, is there any option to add in this script.

    Thanks & Regards,

    Posted 16 Mar 2010 at 12:04 pm
  3. JC wrote:

    Nice to see it is useful. ;)
    Unfortunately, the script doesn’t support yet SSL, but I will think about it for a future release.

    Posted 16 Mar 2010 at 1:33 pm
  4. nfl store wrote:

    I think it’s useful for me.Thank you for sharing.

    Posted 30 Apr 2010 at 10:31 am
  5. Bruno wrote:

    I have been having the same problem.
    Thanks a lot for this.
    Yet, I tried it the script breaks when trying to extract the attachment.

    “Can’t fetch the message ! at attachment stripper.pl line 36.”

    Do you have any tips about what could be the problem?
    I’m using this in a Dreamhost server, running locally on the server.

    Thanks a lot for the great work you made here!
    Bruno

    Posted 23 Sep 2010 at 10:29 pm
  6. JC wrote:

    Hmm… It means that it could connect correctly to your mailbox but failed at parsing the message body.
    It is difficult to tell what’s wrong without seeing the message structure itself.
    Have you tried with other messages?

    Posted 23 Sep 2010 at 11:45 pm
  7. Bruno wrote:

    Hi JC.
    I tried with simple emails where the attachment is just a txt file and I tried as well with jpg.
    Do you have the time to try it yourself and see if it works?

    Thank you in advance.
    Bruno

    Posted 28 Sep 2010 at 11:04 am
  8. Chris wrote:

    Hi,

    nice work. Does it work with attachments in a mail sent from an iPhone, because these doesen’t work with most scripts.

    Thans
    Chris

    Posted 08 Dec 2010 at 10:37 am
  9. phocean wrote:

    @Bruno: I really don’t have time but I can have a really quick look (5 min), if there is a chance…

    @Chris: I haven’t tested it and can’t as I don’t have an iphone.
    Doesn’t the iphone respect the RFC ?

    Posted 06 Feb 2011 at 10:34 am
  10. Sebastian wrote:

    hello,

    i treid to get this trunning under Linux and installed IMAPTalk.pm. Script seems to login but it get stuck with this error message

    Connecting to IMAP server at 127.0.0.1:myusername…
    Please report to rjlov at /lib/Mail/IMAPTalk.pm line 3859.
    Warning only: IMAPTalk not using unicode_folders at /lib/Mail/IMAPTalk.pm line 3860
    Mail::IMAPTalk::_fix_folder_name(‘Mail::IMAPTalk=HASH(0x83a9ca4)’,'INBOX’) called at /lib/Mail/IMAPTalk.pm line 1168
    Mail::IMAPTalk::select(‘Mail::IMAPTalk=HASH(0x83a9ca4)’,'INBOX’) called at ./imap.pl line 31
    main::connexion(127.0.0.1,’myusername’,',mypass’,143,’INBOX’) called at ./imap.pl line 96
    Please report to rjlov at /lib/Mail/IMAPTalk.pm line 3861.
    IMAP connection successful !
    mkdir extracted/msg-1314173768-21507-0: No such file or directory

    Posted 24 Aug 2011 at 10:27 am
  11. Sebastian wrote:

    OK i had to mkdir the folder “extracted” on my own. The script works and extracts messages very well. But the error message remains. i could live with it but would like to know what it menas. …

    Posted 24 Aug 2011 at 10:55 am
  12. phocean wrote:

    Hmm you can safely ignore this warning.
    In my script, I specify the use of unicode, for compatibility with most of languages :

    use Mail::IMAPTalk qw(:utf8support);

    My guess is that your imap folder is not in unicode encoding. Am I correct ?

    Posted 26 Aug 2011 at 3:46 pm
  13. Sebastian wrote:

    Yes! it is not unicode. But doesn’t matter. Your script works perfectly for me. Thanks!

    Posted 30 Aug 2011 at 5:02 pm
  14. onlyformydream wrote:

    I need to strip ALL the new emails with attachments instead of the latest new email. What should I do to process the array of @MsgId (and not $MsgId)?

    I also noticed that the script stripped the attachment from the email, but doesn’t flag it “read” afterwards, so if there is no new email come in and you re-run the script, the same email attachment will be extracted again.

    Sorry for my noob questions and thanks you in advance for your help!

    Posted 02 Nov 2011 at 12:54 am

Post a Comment

Your email is never published nor shared. Required fields are marked *