procmail – a simple exampleprocmail is a tool for manipulating mail. If you like to sort your incoming mail into various folders/directories, procmail can do that for you. If you want to cut down on the spam, procmail can help with that. Basically, if you can write a rule for it, procmail can do it.
I wanted to sort incoming mail for a list archive box. Mail for one list had to go to one mailbox and from another list for another mailbox. This seemed pretty easy to do with procmail.
The home page for procmail is http://www.procmail.org/.
Here’s a link to procmail in FreshPorts.
And I used some of this FAQ. But Timo’s procmail tips and recipes was very useful once I started creating my procmail rules.
InstallationThe first step was to install procmail from the ports. I followed the instructions found in the FreeBSD handbook for compiling ports from the internet.
cd /usr/ports/mail/procmail make install
Don’t do a make clean until after you grab the examples during the configuration later in this article
procmail makes use of .forward to process your mail according to the rules you specify within .procmailrc.
Here is what I put in ~/.forward:
[dan@set:/home/dan] # more .forward "|exec /usr/local/bin/procmail || exit 75"
The above is taken from man procmail, in the NOTES section. Be sure to include the quotes.
procmail rulesHere is the example rule set I’m using. This example was taken from
Your procmail version may vary so the above directory may differ on your box.
[root@set:/home/dan] # more .procmailrc # Please check if all the paths in PATH are reachable, # remove the ones that are not. PATH=$HOME/bin:/usr/bin:/usr/ucb:/bin:/usr/local/bin:. MAILDIR=$HOME/mail # You'd better make sure it exists DEFAULT=$MAILDIR/mbox LOGFILE=$MAILDIR/from LOCKFILE=$HOME/.lockmail :0 # Anything from Henry * ^From.*henry henries # will go to $MAILDIR/henries:0 # anything from cvs-all mailing list * ^Sender.*owner-cvs-all@FreeBSD.ORG lists/cvs-all # will go into my archive # Anything that has not been delivered by now will go to $DEFAULT # using LOCKFILE=$DEFAULT$LOCKEXT
Be sure to create the mail directory indicated above:
That should be about it. Try it and see.
TestingSometimes it can be difficult to test your procmail rules. You can’t always wait for a mail message which meets your test conditions. Fortunately, there is another way. Take a message, including the headers, and copy it to a file. Then pipe the contents of that file to procmail. I found the testbench as described at Timo’s procmail tips and recipes very helpful.
Clean upRemember to do a make clean!
Recipes (added on 31 December 2000)Gill wrote in with this recipe for mailing lists. It was actually written by Neil Blakey-Milner.
I am sure you will get hounded with people suggesting thier own procmailscripts, but this one came to me by way of the -questions list and it is the niftiest one i’ve ever seen and it works perfectly. It is useful for
all of the FreeBSD lists:
# From: Neil Blakey-Milner
# Cc: email@example.com
# Subject: Re: Procmail recipes for fbsd lists
# I use:
# It can probably be simplified, but it expands as necessary when I
# join new lists, &c.
Chad Ziccardi wrote in with these two URLS: