Howto: receive mail and save attachment with fetchmail, procmail and metamail

At work I recently had to set up a solution that periodically checks a POP3 account on our M$ Exchange wannabe mailserver, and saves the attachments to some folder for further processing. As I didn't find a ready-to-go-solution for this on the web, just snippets here and there, and of course hundrets of other people asking the same, here it is.

You will need

  • fetchmail
  • procmail
  • metamail (or uudeview, see first comment below)

In this example I'll use a POP3 account, the full mail will be backed up to ~/mail_backup, and attachments will be unpacked to ~/attachments. fetchmail also handles IMAP accounts just fine. Please refer to the fetchmail documentation.

Setting up fetchmail

First create a file $HOME/.fetchmailrc

poll my.pop3.server 
protocol pop3 
user 'myuser' 
password 'mypassword' 
mda '/usr/bin/procmail -d %T'

Setting up procmail and metamail

Then we configure procmail so it forwards the messages to metamail in $HOME/.procmailrc

:0
*^content-Type:
{
        # backup the complete mail first..
        # you can leave out this part if you don't want a backup of the complete mail
        :0c:
        $HOME/mail_backup

        # Now the actual unpacking part
        #
        # this is the place where the attachments will be unpacked to
        METAMAIL_TMPDIR=$HOME/attachments

        # forward to metamail
        :0fw
        | metamail -w -y -x
}

Regarding metamail, we tell it to ignore any mailcap file, so it doesn't use interpreters (-w), yanking the message and save the content raw (-y) and force it in non-interactive mode (-x).

That's it about it. We are ready for testing.

Test run

Now we simply fire up fetchmail, the rest should be magic.

fetchmail -kv

HTH

Tags: 

Comments

For more decoding joy, or if you can't run metamail (such as on a CentOS machine) you can use uudeview instead. All you have to do is install uudeview and change the .procmailrc file to

:0
*^content-Type:
{

   # backup the complete mail first..
   # you can leave out this part if you don't want a backup of the complete mail
   :0c:
   $HOME/mail_backup

   # Now the actual unpacking part
   # forward to uudeview and unpack attachments to $HOME/attachments
   :0fw
   | uudeview -p $HOME/attachments -
}

This works well with attachments in Latin,
but the receipt of the letter with attachments in the Cyrillic alphabet
in $ HOME/attachments to be not recognized the names of files

Hi this page is treasure.
saved me from a lot of pain figuring out how to do exact scenario.

i tried mutt, alpine
but this is just a breeze working

thank you

Awesome works for me!

Thanks! that procmail/metamail recipe is exactly what I was looking for!

metamail replacing the attachments name, how I can retain the original attachment file name ?

That's what -y is for in metamail. The original filename is kept here. But google reveils other cases were it doesn't work, too. So maybe try uudeview as explained in the comment above. It seems way smarter than metamail, looking at the man-page.

This really helped me with automating the extraction of attachments with a fetchmail+procmail setup I already had going. Much appreciated, cheers!

This is not really good which it reaname the files with some unknown chracters
Is there any way to retain the original file name by metamail