procmail

Introduction

Procmail is a mail delivery agent (MDA) capable of sorting incoming mail into various directories and filtering out spam messages. Procmail is widely used on Unix based systems and stable, but no longer maintained; users who wish a maintained program are advised to use an alternative MDA, such as maildrop.

Common operations carried out with procmail include filtering and sorting of emails into different folders according to keywords in from, to, subject or text of the mail, or sending autoreplies, but more sophisticated operations are also possible.

Configure procmail

You do not need to configure procmail to use UBIT email. The following advice is presented for educational purposes only.

  1. In your home directory, create a directory named .procmail:
    
    % mkdir .procmail
    
    
  2. In your home dirctory, create a file named .procmailrc:

    
    % vi .procmailrc
    
    

  3. Copy these six lines into your .procmailrc:
    
    SHELL=/bin/sh
    PATH=/usr/local/bin:/util/bin:/usr/ucb:/usr/bin
    VERBOSE=off
    MAILDIR=$HOME/Mail
    PMDIR=$HOME/.procmail
    LOGFILE=$PMDIR/log
    
    

    Optional: If you only use pine to read mail (you don't use graphical clients like Thunderbird), change the MAILDIR variable to point to lowercase mail:

    MAILDIR=$HOME/mail
    
  4. Optional: If you use pine *and* graphical IMAP clients to read mail, create a symlink called mail to point to your Mail directory. Our IMAP daemons place your email in ~/Mail. But pine looks for messages in ~/mail. A symlink from ~/Mail to ~/mail enables both types of mail readers to access your email.

    
    ~% ln -s Mail/ mail
    ~% ls -al mail
    lrwxrwxrwx   1 cwmiller        5 Oct 30 10:31 mail -> Mail/
    
    

  5. Create a .forward file in your home directory. It must contain *only* this line:

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

    Replace USERNAME with your username.

    For your .forward to actually work, make sure only *you* have write permission over your home directory and your .forward file. If your home directory is group-writable or world-writable, the sendmail mail delivery program will ignore your .forward file. To be safe, remove all unwanted write permissions:

    
    % chmod go-w ~/.*
    % chmod go-w ~/*
    
    

  6. Send email to yourself to make sure it doesn't bounce.

Recipe Rules

Recipes work by "regular expression" pattern-matching. A full description of regular expressions is beyond the scope of this page, but a few points will get you started:

  • The "." character will match any single character unless you put a "\" character in front of it. "Escaping" it with a "\" makes it match only the "." (dot) character.
  • The "*" character means 'Any number of the thing to the immediate left of the "*"', so "r*" means 'any number of "r" characters'. Similarly ".*" means 'any number of any character' since "." will match any character - use that with caution!
  • A "!" means logical "NOT".

The lines immediately following each ":0:" line make up a recipe. Separate each recipe from the next one with a blank line. Inside recipe lines that begin with "*" are regular expressions procmail should use to check each message. The last line of each recipe says what to do with the message if it matches the recipe.

procmail processes .procmailrc recipes from the top of the file to the bottom. Once it matches a recipe, it stops processing that message. So if you receive desirable bulk email from a Listserv, and you don't want the X-DCC recipe to file messages from it in your Bulk mail spool, you must add a recipe above the X-DCC recipe to handle that email as a special case. Examine the full email headers of a message from the Listserv for some sort of special indicator that this message came from a Listserv. Depending how the Listserv administrators have decided to handle the list, there may be special headers added, or you may need to use the "From" or "To" header lines.

Sample Recipes

Filter 'Good' Bulk Mail

The BIND-Announce Listserv adds an X-List-ID header tag to its messages:

X-List-ID: <bind-announce.isc.org>

Filter bulk mail received from the BIND-Announce Listserv to your default mail spool file (if this recipe is placed above the X-DCC recipe):

:0:
*X-List-ID: <bind-announce\.isc\.org>
$ORGMAIL

Filter Bulk Mail to your Default Mail Spool

Route bulk and individual mail received from cert-advisory@cert.org to your default mail spool file (if this recipe is placed above the X-DCC recipe):

:0:
*From: .*<cert-advisory@cert\.org>
$ORGMAIL

Filter Messages Based on Keyword and Source

Route mail received from sources not in *.buffalo.edu that contain the word 'Hoodia' in the message Header or Body into a folder called 'Spam':

:0 HB
*!^From:.*buffalo\.edu$
* .*Hoodia.*
Spam

Sample .procmailrc File

We include a .procmailrc-sample file (attached below) to demonstrate some procmail rules that you might like to adopt. Remember that procmail reads the .procmailrc file from top to bottom; as soon as it hits a rule that matches a mail message, it applies that rule and goes no further. Order of precedence matters!

Notes

  1. procmail uses a special variable called $ORGMAIL (ORiGinal MAILbox) to represent your default mail spool file.
  2. At the top of your .procmailrc, set VERBOSE=on to log the rules that have matched or filed each incoming message.

References

  1. http://en.wikipedia.org/wiki/Procmail
  2. http://www.procmail.org/
  3. % man procmail
  4. % man procmailex
  5. % man procmailrc
  6. http://www.faqs.org/faqs/mail/filtering-faq/
  7. http://lipas.uwasa.fi/~ts/info/proctips.html
AttachmentSize
procmailrc.txt369 bytes
forward.txt68 bytes
procmailrc-sample.txt3.79 KB