Software

Set up e-mail filtering with procmail

Find out how to set up basic e-mail filtering in Linux with the procmail Mail Delivery Agent (MDA).

Procmail is a powerful Mail Delivery Agent (MDA) that can be used for any number of things including personal mail filtering, mail delivery on servers, and much more. More often than not, individuals using fetchmail to download e-mail will use procmail to file and deliver it to its final destination—typically an mbox file somewhere on the user's hard drive.

Many users have GUI e-mail clients that can read standard mbox files, but because of procmail's flexibility, they opt to use fetchmail and procmail to download and filter their mail rather than their e-mail client.

Tips in your inbox
Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills.
Automatically sign up today!

For instance, procmail can filter messages based on pretty much any criteria you for which you can write a regular expression. If you wanted to filter mailing list messages into separate mbox files, you could do so based on the Return-Path e-mail header like this:

<code>
MLIST=/Users/joe/Mail/MailingLists
:0:
* ^Return-Path:.*security-discuss-owner@mandrivalinux.org
$MLIST/Mandriva/security-discuss
:0:
* ^Return-Path:.*security-firewall-owner@mandrivalinux.org
$MLIST/Mandriva/security-firewall
</code>

This snippet of a ~/.procmailrc file uses the regular expression denoted by the line beginning with an asterisk (*); in this case, we are looking for a line in the e-mail message that begins with Return-Path, has any number of spaces or characters, and also contains the e-mail address we want to base the filter on. The next line contains the path of the mbox to which the message delivers if it matches.

The final line in a .procmailrc might look like:

<code>
:0
/dev/null
</code>

This is a fairly drastic measure, but essentially it means that if none of the previous criteria matched, deliver the message to /dev/null, which would discard the message entirely. A more likely scenario would be to change this to something like /Users/joe/Mail/Inbox where all unfiltered mail would go.

Procmail is so powerful that these expressions are called recipes, and recipes have even been generated to discard spam. You can also rewrite parts of messages based on the content. More information on procmail can be found at www.procmail.org, and you can install it from your Linux vendor's installation medium; procmail comes with all Linux distributions.

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

0 comments