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.

4 thoughts on “Extract Email Attachments With Procmail and Munpack

  1. Pingback: Extract Email Attachments With Procmail and Munpack « blog.lundscape.com | Markuksen www-sielunmaisema

  2. Krut

    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.

  3. Zachary Lund Post author

    @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. :)

  4. Txoof

    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…

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>