Software

Filter mail with powerful procmail

Procmail is perhaps one of the most powerful mail filtering programs available. It plugs in seamlessly with fetchmail and can do an amazing number of things, including simply filtering mail into various mailboxes, forwarding mail to another account, or executing programs.

Procmail is powerful and complex, and can be intimidating to those who have never used it before. However, no other mail filtering support built into an e-mail client can compare to it.

To begin, procmail uses ~/.procmailrc by default. In the interest of organization, and because you can use multiple files to create your rule set, it may be better to create a directory called ~/.procmail/ with the file ~/.procmail/procmail.rc as the primary configuration file and symlink ~/.procmailrc to it. For instance:

$ mkdir .procmail
$ touch .procmail/procmail.rc
$ ln -s .procmail/procmail.rc .procmailrc

A very simple ~/.procmail/procmail.rc may look like:

DATE=`date +%Y-%m`
SHELL=/bin/sh
VERBOSE=off
PMDIR=/home/user/.procmail
LOGFILE=$PMDIR/procmail.log
LOG="
"
MAIL=/home/user/Mail

:0 Wh: msgid.lock
| formail -D 8192 $PMDIR/msgid.cache

:0 c
$MAIL/Archives/incoming-$DATE

INCLUDERC=$PMDIR/deletes.rc
INCLUDERC=$PMDIR/mailinglists.rc

:0
* ^TO_.*user@school.edu
$MAIL/Inbox-school
:0
$MAIL/Inbox

This may look complicated, but it's quite straightforward. The first few lines define various variables, just like one would find in a shell script. Of particular interest are $PMDIR, which defines where the various procmail-related files live (~/.procmail/). Also, $LOGFILE defines where procmail logs its actions. When debugging or starting out, you will want to set $VERBOSE to "on" in order to capture as much information as possible.

The $LOG variable defines the separator between log entries; in this case it inserts a blank line. Finally, $MAIL determines where mailboxes live. Not all of these variables are specific to procmail — some are variables that are reused in later recipes. The ones specific to procmail are $SHELL, $VERBOSE, $LOGFILE, and $LOG.

The first recipe creates a cache of recently-received Message-ID's of e-mails and weeds out duplicate messages. In this case, the cache is in the file ~/.procmail/msgid.cache and will remain approximately 8KB in size.

The second recipe creates a copy of every single message received and stores it in a by-month dated mailbox. With this recipe, even if procmail does something unexpected, you retain all of your e-mails. With it, you can read and delete e-mails with impunity knowing you can always go back to find a message at any time.

The next two lines set the $INCLUDERC variable, which tells procmail to include the noted files at that point in processing. In other words, after the first two recipes, procmail will proceed through the recipes in ~/.procmail/deletes.rc and then ~/.procmail/mailinglists.rc before proceeding further.

Finally, the last two recipes define final-destination delivery. With the first, if the e-mail is addressed to user@school.edu (be it in To: or CC:), the mail will be filtered into the ~/Mail/Inbox-school mailbox. All other messages that have not met with any other rules get filtered into ~/Mail/Inbox.

Next week, we will look further at what procmail can do.

Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks