Panix: Public Access Networks Corp.

Using Procmail to Filter Your Email on Panix

If you ever need to disable procmail in a hurry the shortcut to turn it off is to disable forwarding. You can do this by running
forward -d
from the command line,
or by running
forward -e
from the command line and adding a "#" to the beginning of the line that contains "procmail",
or you can
log into webmail, go to the Options page and select Mail Forwarding Options, and modify your forwarding instructions there.

About Procmail

Procmail is a powerful mail processing program (hence the name) that allows you to filter and sort your mail as it is delivered to your account. This means that it's already sorted by the time you get it, so you save on retrieval time. Procmail can be programmed to forward selected messages to a different address, or to sort mail into different folders on the basis of return address, subject line, or just about any other criterion you can think of. It can even be used to cause special headers to be modified or added to certain messages. It's a powerful and fairly complicated program, and this page doesn't attempt to explain it in detail, but it should serve as an introduction to how you might use it at Panix.

Procmail is invoked automatically when you run it from your forward file.
NB: This is not a .forward file in your home directory space.
It's a file on the mail server that you can modify from webmail (Options->Mail/Forwarding Options) or from the shell using the command "forward -e".

To invoke procmail, your forwarding file should contain this line, without quotation marks:

| /usr/local/bin/procmail -t

We have created an installation and an update script that you can use if you want to use our default setup or need only minor modifications. If you are already familiar with procmail, you might want to set things up yourself. That's okay, but we have the following suggestions for those who take this route:

Note that our main spam-filtering resource is SpamAssassin, SpamAssassin, and it can be invoked directly from webmail, so you don't need to use Procmail for this purpose, although you might choose to do so.

Why should I use Procmail?

You should use procmail if

Setting Up Procmail

Panix has a script, install-procmail, that will set up procmail for you. This script provides

If you're already running Procmail but not SpamAssassin, and you'd like to run SpamAssassin from Procmail, this line will do it:
INCLUDERC=/net/local/filters/rc.spamassassin
Make sure that you put that line after any rules that whitelist addresses. If you run vacation from procmail, you probably want to run SpamAssassin first, so you don't send your vacation reply to spammers.

If you're familiar with procmail and want to set it up on your own, that's fine, but we recommend that you not set DEFAULT or ORGMAIL unless you're sure you know how to set them for the way you want your mail handled at Panix.

Old filter sets

In the past, we have offered some lists of sites or IPs that were known to generate spam. Those lists are still available, but they are deprecated and are no longer being updated. For filtering, we recommend SpamAssassin (either webmail-based or procmail-based), and for more the aggressive approach of bouncing mail at the server (so it's not delivered to your account at all), we suggest the Advanced Anti-Spam Options available from the Options page of webmail.

The .procmailrc file

The .procmailrc file holds your procmail settings. If you want (some) mail filtered into folders other than your inbox, it specifies the default directory for those folders. It provides a place for you to you set variables for procmail to use, and a means of specifying where you want your procmail log to be stored, and the means of including other rule sets, your own or shared sets, in your procmail implementation.

One of the most important things that happens in your .procmailrc file is the setting of your TRASH variable. If you tell the install script that you want to delete messages unread, this will be set to /dev/null, and any messages sent there are not recoverable. You have to weigh the risk of losing mail you need against the risk of having a trash folder that grows so large it interferes with your quota. (The default setting for TRASH is .maildir/.Trash/. If you use that, messages will be deleted from the folder when they've been there one week.)

However, you can modify your .procmailrc file so that TRASH is set differently for different rules. The setup script lets you save virus-laden mail and suspected spam to different folders, although, by default, both are set to .maildir/.Trash/, the Trash folder you see from webmail. You'll find more detailed information on setting variables below.

The .procmail directory is, by default, where Panix users' keep their procmail logs and any of their own rule sets. If you use the default setup, your procmail logs will be rotated to prevent them from growing too large and exhausting your disk quota.

The line that lets you run spamassassin from procmail is this:
INCLUDERC=/net/local/filters/rc.spamassassin
As noted, you can run SpamAssassin either from webmail or from procmail. It's not a good idea to do both because that wastes system resources and, should you ever need to check, makes it harder to figure out what's happening to your mail.

The line that lets you run virus checking from procmail is this:
INCLUDERC=/net/local/filters/rc.viruscheck
You can also run virus checking from webmail or from procmail. The virus-checking option is located on the Options page of webmail.

What's in the .procmailrc file?

This is a sample of our default .procmailrc file:
   #Set on when debugging
   VERBOSE=off

   #The directory where your mail folders will be stored.
   MAILDIR=$HOME/.maildir

   #Directory for storing the procmail log and the rc files
   PMDIR=$HOME/.procmail
   # The log of procmail actions
   LOGFILE=$PMDIR/log

   # SPAMTRASH is used to tell SpamAssassin where to deposit the spam
   SPAMTRASH=$MAILDIR/.Trash/
   
   # VIRUSTRASH is used to tell the virus filter where to deposit the virus mail
   VIRUSTRASH=$MAILDIR/.Trash/
   
   # Virus check comes before all other filtering, since virus mail could be
   # from someone you know
   TRASH=$VIRUSTRASH
   INCLUDERC=/net/local/filters/rc.viruscheck
   
   # Your own filtering rules
   INCLUDERC=$PMDIR/rc.folders
   
   # Spam filtering
   TRASH=$SPAMTRASH
   INCLUDERC=/net/local/filters/rc.spamassassin

Lines beginning with # are called "comments". They're intended for people, not for computers.

The words in upper-case are variables. You'll notice that variable names to the righ of an equal sign are preceded by $. This tells the system to look at the value of that variable. For example, you see the setting
PMDIR=$HOME/.procmail
which sets your "procmail directory" to the value of .procmail in your home directory ($HOME). Then you can set your log file this way:
LOGFILE=$PMDIR/log
using the value of $PMDIR that you have set.

Lines beginning INCLUDERC= set a special variable that tells procmail to process the specified file.

Setting the MAILDIR Variable

One important variable is MAILDIR. This is the default directory for procmail. If you set MAILDIR and then simply name a folder in your recipes, that folder will be in the directory you specify as MAILDIR.

If you want to save your mail to folders that are visible from webmail or IMAP, you should set MAILDIR to $HOME/.maildir, like this:

MAILDIR=$HOME/.maildir
If you want to save your mail to the mail directory in your home directory space (which is the default for Pine), you can specify
MAILDIR=$HOME/mail
You can override your MAILDIR setting in any recipe. For instance, if you want most of your mail saved to your "mail" directory by your spam to your webmail/IMAP Trash folder, you can set MAILDIR=$HOME/mail and then specify TRASH=.maildir/.Trash separately. Or you might want most of your mail saved to $HOME/.maildir (so that's your MAILDIR setting), but not mail from one particular address, which you want to save to a special file. We'll give more examples below. It's often useful to make your own filtering rules, to filter spam or just to sort mail into different folders. The setup script provides a place for them:
INCLUDERC=$PMDIR/rc.folders
It's empty unless you put rules into it. You can also create other rule sets and add INCLUDERC lines to your .procmailrc file to have procmail run them.

You need to bear in mind that rules will be processed in the order in which they occur. So if you want to save mail with "purple" in the subject line to a special folder whether or not SpamAssassin thinks that message is spam, you have to make sure your rule about "purple" precedes the SpamAssassin rule. On the other hand, if you want to filter out the spam first and then save your "purple" mail to a special folder, you have to make sure the SpamAssassin rule comes before the "purple" rule.

If you look at your .procmailrc file, you'll see that we put the INCLUDERC line for your own procmail recipes before the INCLUDERC line for SpamAssassin. That's to give you a place, all prepared, for you to use for whitelisting addresses. (The webmail version of SpamAssassin has web-based control of SpamAssassin's own whitelisting facility, and you can modify your .spamassassin/user_prefs file to include whitelisted addresses as well, but many people prefer the flexibility of procmail rules for this purpose.)

Procmail logging

By default, our procmail setup script tells procmail to log what it does with your mail to a file in your .procmail directory called log. As noted, if you use the default settings, Panix rotates this log to prevent it from growing too large.

Typically, an entry looks something like this:

From user@example.com  Thu Sep  1 12:45:02 2005
 Subject: Lunch tomorrow?
  Folder: /net/staff/home/youraccnt/.maildir/new/1125593105.7251_1.panix1.pan 1044
By looking at the log, you can see whether a message that you're looking for got to your account.

You may have noticed this line in the .procmailr file:

 
   VERBOSE=off
Should you ever need to figure out exactly what is happning to a message, you can change that setting to
   VERBOSE=on
and bounce the message to yourself. (If you do that, you should set the message aside first and edit out any of the extra headers added by your own mail filtering, or they'll interfere with the processing. You should also modify the message-ID so it isn't exactly like the original. You can easily do this by adding "X" or "was" to the actual ID (inside the angle brackets), so that something like this:
Message-ID: <20051006173459.GA11872@panix.com>
becomes
Message-ID: <was-20051006173459.GA11872@panix.com>

If you turn on verbose logging, you must not forget to turn it off as soon as you've finished testing. Verbose logging can be very verbose, and takes up a great deal of space.

Procmail recipes

A procmail recipe checks each message for the characteristic(s) that you specify and then disposes of the message accordingly.

Folder types

Before you tell procmail how to dispose of your email, you have to know where you want it to go, and that includes the type of folder you're using.

Folders that are accessible from webmail are in a format called Maildir++. (Yes, it's confusing. There's the procmail variable MAILDIR, there's the Maildir++ folder type, and there's your .maildir directory, or, more accurately, the symbolic link in your home directory to your mail on the server.)
The folders that you may have used with Elm, Mutt, Pine, and certain other Unix shell mail readers are in mbox format.

Each folder in mbox format is a single file. Messages are separated internally by the string "From " at the beginning of a line. Each Maildir++ folder is actually a directory, or, more accurately, a directory subtree. Each message is a separate file. You have to be careful to specify the right type of folder, and to make sure you get the path the way you want.

Recipes for mbox folders

If you have MAILDIR set to $HOME/mail, the following recipe will save any message with "Testing" in the Subject: line to $HOME/mail/testing:

:0:
* ^Subject:.*\<testing
testing

You will note that matching upper and lower case isn't important. This recipe will not match mail with "contesting" or "protesting" in the subject line because of the \<, which marks the beginning of a word boundary.

If you have MAILDIR set to $HOME/.maildir, you can still save mail to $HOME/mail/testing this way:

:0:
* ^Subject:.*\<testing
$HOME/mail/testing

Note that there is a colon after the zero on the first line of each of these recipes. That tells procmail to lock the folder so that it won't become corrupted if more than one message arrives at once.

Recipes for Maildir folders

If you have MAILDIR set to $HOME/.maildir, the following recipe will save any message with "Testing" in the Subject: line to a folder that shows up from webmail or IMAP as "testing":
:0
* ^Subject:.*\<testing
.testing/

Note that the folder name begins with a dot (which is necessary so that the folder is visible to the IMAP server) and ends with a slash (which is necessary to tell procmail that it's saving to the maildir++ format).

If you have MAILDIR set to $HOME/mail, you can still save mail to $HOME/.maildir/.testing this way:

:0
* ^Subject:.*\<testing
$HOME/.maildir/.testing/

Again, note that the foldername has to begin with a dot and end with a slash.

Note that there is no colon after the zero on the first line of each of these recipes. As each message is a distinct file, there is no need for a lock.

You will note that matching upper and lower case isn't important. This recipe will not match mail with "contesting" or "protesting" in the subject line because of the \<, which marks the beginning of a word boundary.

Additional Resources

Procmail is a very powerful program with a lot of features. The Panix help desk can provide support only for some basic features. The Panix user community includes a number of extremely knowledgable procmail users who are most generous with their expertise. You can generally find them in the panix.questions newsgroup.

Meantime, here is a list of resources to help you get familiar with procmail, from basic tutorial-level to advanced tricks.


The man pages.
Procmail has several useful man pages. Check out
man procmail
Technical details.
man procmailrc
How the procmailrc file works.
man procmailsc
Using Procmail's scoring
man procmailex
Examples - probably the most useful of all.
Mail Filtering with Procmail: A Tutorial
Ian Soboroff's (of Univ. of Maryland Baltimore County) excellent getting-started essay. Thanks to his clarity, and the fact that UMBC uses basically the same set of paths as Panix, we can find nothing of substance to add.

Timo's Tips
In FAQ format, lots of answers and yet more links to other useful material.

Procmail Quick Start
This page, copyright Nancy McGough & Infinite Ink, contains lots of details and many helpful links.

The Official Procmail Web Site
For the truly fascinated or obsessive Procmail devotee, here's where you can find more tutorials, mailing lists for Procmail discussion or general mail-filter theory, tips and tricks for creative Procmail recipe tweaking, release notes, feature request and bugfix lists, or even the latest source code to install Procmail on your Linux box at home.


© Copyright 2003,2005 Public Access Networks Corporation

webmaster@panix.com | Last modified: Thursday, 27-Mar-2008 11:00:39 EDT

[ Panix Home ] [ Help System Index ] [ Email and Spam Help ] [ Top of This Page ]