How can I use procmail to filter my UNIX e-mail?

Procmail can be used to filter all of your incoming mail into folders that you specify. These filters (sometimes called recipes) are written in text and stored in your home directory. Since there are many features to procmail, this FAQ will only cover the most common ones. Please read the man pages by running man procmail, man procmailrc and man procmailex for more information and examples.


Sample Recipes

How to setup your .procmailrc:

Create a file in your home directory, ~/.procmailrc.

You can have variables and comments in procmail like so:

# My variables
HOME=/export/home/myaccountname
MAILDIR=$HOME/mail

# neccessary variables
PATH=/usr/local/bin:/usr/bin
VERBOSE=no
LOGABSTRACT=yes
COMSAT=no

LOGFILE=$HOME/procmail.log
Please run 'which procmail' before you make your .procmailrc, and make sure that the directory that procmail is located in (as well as any other programs you may wish to use) are in the PATH variable.

If you wish to have logging, set LOGFILE to a valid file. You can always turn off loggin by simply removing or commencting out the line that sets the LOGFILE variable.

VERBOSE and LOGABSTRACT are logging options (see man procmailrc), and we recommend setting COMSAT to no.

How to filter by sender:

:0
* ^From:.*trolley@neighborhood.com.*
mr_rogers_neighborhood

The first line is the recipe starting line. On this line you can specify options for the recipe. Normally, once a match is made in procmail, the message would stop at the recipe and exit out of procmail. If you want a copy to be made in both mr_rogers_neighborhood as well as trolley, you would put a c after the 0:

:0 c
* ^From:.*trolley@neighborhood.com.*
mr_rogers_neighborhood

:0
* ^From:.*trolley@neighborhood.com.*
trolley

Note that the second line in the second recipe still has a condition to look for "trolley@neighborhood.com". It is not necessary for the two recipes to follow each other. The second recipe may be the last one in the text. However, the second one cannot proceed the first for obvious reason.

How to filter by recipient:

:0
* ^(To|CC|Cc|cc):.*myaddress@seaseme_street.com.*
my_mail

The reason why "(To|CC|Cc|cc)" is written in "or" format is because you are never sure where the address is likely to be. This line says, "if myaddress@seaseme_street.com appears in To, CC, Cc, cc, deliver to my_mail. Note that some mailing clients label CC as Cc or cc, hence the rule.

How To filter by subject

:0 D
* ^Subject:.*How To get to.*
directions

This filter will look for "How To get to" in the subject line, in exactly that case. The "D" flag makes the search case sensitive, where by default, it is case insensitive.

How to filter with multiple conditions:

You should have noticed that all of the conditions start with a *. You can narrow the recipe by adding more conditions:

:0
* ^(To|CC|Cc|cc):.*myaddress@seaseme_street.com.*
* ^Subject:.*sponsored by.*
letter_sponsor

This filter will filter by recipient as well as by subject.


Please check the man page for procmailrc for more information about how to write your .procmailrc.

After you have completed all of your rules, you must edit your .forward on the machine you receive your mail on so it reads:

"|/usr/local/bin/procmail -t #username"

where username is your username. You must include the quotes. (Junkfood users: as long as you put this .forward in your home directory, you'll be fine, irrespective of what machine you're working on.


Last updated May 6, 2002