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 :

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. ;)

14 thoughts on “How to strip the attachment from an IMAP mail

  1. octopus

    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,

  2. JC Post author

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

  3. Bruno

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

  4. JC Post author

    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?

  5. Bruno

    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.

  6. Chris


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


  7. phocean Post author

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

  8. Sebastian


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

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

  9. Sebastian

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

  10. phocean Post author

    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 ?

  11. onlyformydream

    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!

Leave a comment

Your email address will not be published. Required fields are marked *