Swarthmore College Department of Computer Science

using procmail

From http://www.procmail.org/

Procmail can be used to sort your incoming mail into separate folders/files (real convenient when subscribing to one or more mailing lists or for prioritizing your mail), preprocess your mail, start any programs upon mail arrival, or selectively forward certain incoming mail automatically to someone.

I use procmail to sort my incoming mail into predefined files, making it easier to read my mail. For example, I (user knerr) am subscribed to two or three mailing lists that each send out 10-20 emails per day. Without procmail, all of these messages appear in my standard mailbox (/var/mail/knerr). Using procmail, I have each list's emails automatically sent to a different file. Then, when I am ready to read the messages from a certain list, I just open that file with my mail-reading program (e.g., mutt -f list1).

To start using procmail, you need to do three things:

1. Set up procmail (create a .procmailrc file)
2. Set up your filtering recipes (in the example below, the rc.lists file)
3. Set up procmail to process your incoming mail (with a .forward file)

There is also a lot of information in the procmail, procmailrc, and procmailex man pages. You should probably read them briefly before proceeding (i.e., if you set up procmail incorrectly, you can loose mail or create infinite mail loops).

Create a .procmailrc file. This file contains variables that tell procmail how to run.

#########################################################
# ~/.procmailrc
#
# set on when debugging
VERBOSE=off
#
# home mail directory (I use mdir, you can use whatever you
# want....just make sure the directory exists)
MAILDIR=$HOME/mdir
#
# directory for storing procmail log and rc files
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/logfile
# which recipe file to use
#INCLUDERC=$PMDIR/rc.testing
INCLUDERC=$PMDIR/rc.lists
#########################################################

Create the procmail dir and recipe (rc) file. This file tells procmail how to filter stuff. First match wins.

###########################################################
# ~/.procmail/rc.lists   (the filtering recipes, 
#                          do "man procmailrc" for details
#                         and "man procmailex" for examples)

# send debian-sparc list emails to folder $MAILDIR/IN.debian
:0:
* ^TOdebian-sparc
IN.debian

# anything from MAILER_DAEMON, but not those containing
# "Subject: tripwire report", goes to $MAILDIR/IN.MAILERD
:0:
* ^FROM_MAILER
* !^Subject:.tripwire report$
IN.MAILERD

# recipe used for initial testing
# anything from knerr goes to folder ~/mdir/IN.testing
#:0:
#* ^FROM.*knerr
#IN.testing
#
# anything else stays in /var/mail/knerr
########################## end ################################

You should probably create these files if they don't already exist. The touch command is good for this:

% touch ~/.procmail/logfile
% touch ~/mdir/IN.testing

Create (with any editor) a world-readable .forward file to contain the contents shown below. This file sends the mail you receive through procmail, which either filters it to a chosen file, or leaves it in your standard mailbox (e.g., /var/mail/knerr).

% cat ~/.forward
"|/usr/local/bin/procmail"
% chmod 644 ~/.forward

That should do it. Watch the ~/.procmail/logfile to make sure your mail is being procmailed the way you want it. If something is not working, you can just delete the .forward file to stop procmail.