I’ve run my own domain for a long time now, and one of the luxuries this gives me is the ability to give out different addresses of the form <company_name>@mydomain. This is great, because when somebody gives away an email address, I know who did it and can easily block all mail from them. It also allows me to filter messages based on the recipient, so I can easily move commercial mail to categorized mailboxes.

Adding these filters into .procmailrc soon became tiresome, so I decided to get procmail to do this in a way which required no changes to my procmailrc file. I now have two lists - one is a list of senders, the other a list of recipient addresses. Both of these are just in plain text files, and my procmail setup can read them, match against them and put the matched messages into a different Maildir folder.

This is what’s in .procmailrc:

FROMLIST=`formail -xFrom: | sed -e 's/ *(.*)//; s/>.*//; s/.*[:<] *//'`
TOLIST=`formail -xTo: | sed -e 's/ *(.*)//; s/>.*//; s/.*[:<] *//'`

* ? fgrep -qxis $FROMLIST $FROMFILE

* ? fgrep -qxis $TOLIST $TOFILE

The FROMLIST and TOLIST variables contain the list of senders and recipients respectively. I found the sed expression which does this on somebody else’s site, and can’t find it again. If I do, I will add a link and credit here.

The fgrep lines search for a hit from the from/to list in the respective files.

I have the same setup for junk messages (with a recipient list). The result is that I can just add an email address to one of these files each time I want to set up a new rule, and don’t need to touch my procmail recipes.