Setting Up Procmail

Author: jbm <jbm@intertek.net>
Created on February 28, 1999
Last Modified on September 14, 1999
Development stage: Alpha

Procmail is a great tool for sorting your mail under Linux, but it seems to have a bit of a reputation for being arcane. It can be, but with a little help, configuring things is really easy to do!

This document assumes you've got procmail installed - sorry to those of you who don't, but most major distributions have a package for it. It's also very easy to compile.

Please note that the authors of procmail use the word "recipie" to refer to filters. In deference to them, I will do likewise.

Table of Contents

Setting up the .forward file

Place the following line in the ~/.forward file:

"|IFS=' '&&exec /usr/bin/procmail -f-||exit 75 #jbm"
    

The quotes are important! Replace the "jbm" with your username (this is so the file is totally unique; some systems have problems if every .forward isn't unique). This line should be the only thing in the file, unless you're doing some weird stuff with copying your mail (which I doubt you are).

Setting up the .procmailrc file

Beginning the file

Open up ~/.procmailrc in your favorite text editor, for example 'pico ~/.procmailrc' or 'emacs ~/.procmailrc'. pico is a good option for beginners. At the top of this file need to go a few lines:

SHELL=/bin/sh
MAILDIR=$HOME/mail
LOGFILE=$HOME/.procmaillog
VERBOSE=yes
    
Make sure MAILDIR exists! procmail will replace the '$HOME's with your home directory (e.g.: /home/jbm). The LOGFILE is optional, but I recommend it highly. It lets you see if there was mail redirected to /dev/null that shouldn't have been--which is a bad thing. This allows you to debug your regular expressions and/or the order of the recipes you're going to apply. There is an example in the procmailex manpage ('man procmailex') detailing a "safety net" to copy the last 32 messages to a directory for you; this will be covered later.

A recipe in procmail is a set of rules that define a filter. These rules are generally a regular expression to match (e.g.: .*, ^From:.*bob@host.dom.au), and then a line or two tellng procmail what to do when it matches that regexp.

Regular Expressions

Regular expressions are extremely powerful ways to express complex things. They are used for pattern matching (like *.* or foo??.txt, but much more complex). Which makes them really hard to learn. There is at least one book on them and many many books devote chapters and sections to them. We will smooth over things a bit with a few "Regular expression formulas," in the table below:

RegExp | Meaning 
-------+----------------------------------------- 
.      | Matches any character (except newlines) 
^      | Beginning of a line 
.*     | Wildcard: matches anything
    
Yep, those are the only regular expressions you need to know! Now, this is a very simple application of regular expressions, mind you. You should go out and do a little research on them some time, probably when you're learning perl or sed, two very useful utilities.

Recipes

Now that you have regular expressions under your belt :^), it's time to construct a recipe. First, every recipe begins with the line

:0
    
This has procmail scan the headers of the document for matches, use
:0B
    
for body scanning.

The next line is the regular expression procmail should match against. These are typically of the format

* ^Subject:.*fm/news

or

* ^From:.*oths.k12.il.us
    
These lines all begin with "*" and continue on with a regular expression. The next line will be the "folder" to place the mail in; this is just a file in the MAILDIR. Unlike the MAILDIR, procmail will create this file own its own. When all three lines are combined, it looks something like this:
:0
* ^Subject:.*fm/news
freshmeat

:0                 # Guide Mail
* ^Subject:.*uide
jlg
    

procmail evaluates the recipies in beginning-to-end order in the file. procmail compares a message with the first recipie in a file. If the message matches the criteria of the recipie, procmail does what the recipie instructs it to and then stops. If it doesn't match, it tries again with the second recipie, and so on. Therefore, the order you have recipies in does matter! If you want all mail about dogs to go to a dogs folder and all mail from your Significant Other to go to another with all mail from your Significant Other about dogs going to the dogs folder, you had better have the dogs filter above the SO filter.

Comments: comments must not be within your recipies! Just put them on the :0 line, and noplace else. Trust me.

Safety Nets

In case you mess anything up, you should set up a "safety net", as described in the 'procmailex' manpage. Begin by making a 'backup' directory under your MAILDIR. Then add the lines

:0 c 
backup

:0 ic
| cd backup && rm -f dummy `ls -t msg.* | sed -e 1,32d`
    
at the very top of your .procmailrc file. That way, every message gets sent through the recipie. This recipie will keep the last 32 messages sent to you in MAILDIR/backup/.

An Example .procmailrc File

MAILDIR=$HOME/mail
LOGFILE=$HOME/.procmaillog
VERBOSE=yes

:0:                        # UF Mail
* ^Subject:.*Ufies-STAFF
uf

:0:                        # JLG Mail
* ^Subject:.*uide
jlg

:0:                        # FM digest
* ^Subject:.*fm/news
freshmeat

:0:                        # APCi trash
* ^Subject:.*ime Limit
/dev/null

:0:                       # yet more APCi junk
* ^Subject:.*Reached.Hard
/dev/null

:0:                       # cron output
* ^Subject: cron:
cron

:0:                       # SPAM!!!
* ^Subject: .*fast.*cash
/dev/null 
    

Note that I use :0: for the first line; this sets up a "lockfile" so two procmails don't try to write to the same file simultaneously (that would be a very bad thing). Hopefully this makes setting up a .procmailrc easier for you.

Configuring Pine

Of course, it's fine and dandy that you've got your mail all sorted out, but you can't read it from pine this way. Now comes fun stuff. In Pine 4.05 (go grab a copy from http://www.washington.edu/pine/), go to your Folder List from the Main Menu, then to the Incoming-Folders if it's there, and then do Add. It will prompt you for a server, just hit enter. Then it will ask for a folder name. This is what you named it in your .procmailrc, ie: $MAILDIR/foo. Next it prompts you for a nickname, give it one.

As an example, let's say you subscribe to a homebrew list. In your .procmailrc you have:

:0:
* ^Subject:.*PC.homebrew
homebrew
    

Your pine options would then be Folder: "mail/homebrew" and Nickname: "homebrew".

Further configuring things

[more to come here soon, on other things like WMMail and other biff-like apps]

Further Reading

Other things to check out for info:


Copyright © 1999 jbm (jbm@intertek.net). All rights reserved. Permission to use, distribute, and copy this document is hereby granted. You may modify this document as long as credit to me is given.