Extract Email Attachments With Procmail and Munpack

I regularly use the Marquette University PrintWise Canon copiers to scan paper documents into PDF files. These files are delivered to me via email where I manually save each PDF attachment, rename it, and move it to the appropriate directory. Using Procmail and Munpack, I was able to eliminate some of the tedium of this problem.

I inserted the following block of code into my .procmailrc file. It looks for emails that were sent from me and to me by a Canon copier. When it finds one, it copes the email and pipes it to Munpack which extracts the attachments into my attachments directory.

ATTACH=`echo $HOME/attachments`
:0 c
* ^To:.*[email protected]
* ^From:.*[email protected]
* ^X-Mailer: Canon imageRUNNER
| munpack -q -C $ATTACH

After the attachment has been extracted, I can rename it and move it to the appropriate directory, and I still receive the email in my inbox so I know I have attachments to deal with.

6 thoughts on “Extract Email Attachments With Procmail and Munpack”

  1. Thanks Zachary
    It worked. For some reason the I had to create the directory attachment in $HOME.

    Beside that I was wondering is there anyway you can get the name of the attachment file in the same script as I am planning to use the file in another script. ( I.e variable like $attachment_name or something).

    Thanks again
    Krut.

  2. @Krut,

    Do you mean you had to create ~/$HOME/attachments or you had to create ~/attachments? You can alter the directory by changing the $ATTACH variable.

    Unfortunately, it does not appear that munpack will give you the file names. However, if you either always unpack the files in a new directory or clear out the attachments directory when you are done, you could simply iterate through all files in the directory. You could also try piping the email to a script that then calls munpack and performs other actions.

    Let me know how the script works out. I’d be curious to see the finished product. :)

  3. This is almost exactly what I need! I just need something that will *remove* the attachment.
    Do you know if there is a sane way to remove attachments from incoming mail? I want to remove all attachments from mail I pop from Gmail. I’m only interested in backing up the text, google can house all the pictures of babies that family members send around.
    I’ve looked at MimeDefang, but it’s a bit of a bulldozer where as I only need a garden spade. That, and I don’t want to figure out how to run a sendmail backend just to receive email…

  4. I am facing a problem that after I extract the attachment, it is not getting deleted from the server. I am using munpack to extract the attachment. Could you please help?

  5. @Babu, I don’t think munpack is designed to do anything other than extract files. Formail works well for manipulating emails, but I don’t think it can remove attachments, and I think it would only manipulate the downloaded email, not the one on the server.

Comments are closed.