IT management/strategy

Easy Email Filtering with Procmail

October 21, 2009, 12:13 PM — 

There are two ways that procmail can be set up. You can set it up so that each user can choose to use it (or not) or you can have it invoked through your mailer. We're going to examine the first method.

For individual users, there are several steps to setting up procmail. Each new user should first set up his .procmailrc file. This file defines how his email will be processed -- which messages will be dropped into individual mailboxes, which will be deleted immediately, etc. He should then create the required directories and files (more on this in a minute). Third, he should create the .forward file that forces his incoming mail through his procmail filters. And last, he should run a test to make sure that his setup works.

The .procmailrc file contains the settings and filtering rules that procmail will use. The settings in this file might look like this:

# Set to yes when debugging
SHELL=/bin/bash
MAILDIR=$HOME/Msgs       # Make sure this directory exists!
LOGABSTRACT=all
VERBOSE=yes

In this example, we can see that we are specifying the mail directory where the user's filtered messages will be kept. We are also, initially anyway, using verbose mode to capture data in our log file. Once the setup is known to be working properly, it's a good idea to turn verbose logging off:

## LOGABSTRACT=all
VERBOSE=no

The .procmailrc file will also contain a series of rules or "recipes" that determine how messages are processed. The first line of each rule is generally just ":0". This marks the beginning of a recipe, invoking no special options.

The next line generally identifies what you are looking for and can include regular expressions. The recipe shown below, for example, looks for subject lines containing the word "test".

:0
* ^Subject:.*test
testing

The third line identifies the mailbox that you want to append the message to. In this case, our messages will be added to a file named "testing" in the $HOME/Msgs box identified earlier.

Notice that we are filtering on lines that begin (^) with "Subject:" (i.e., the subject line) and then have one or more characters between this text and the word "test".

To delete messages, send them to /dev/null using a rule like this:

:0
* ^Subject:.*v?agra
/dev/null

You can filter your incoming mail by sender, recipient, subject line, message length and key words contained in the message. You can even make some minor changes to the text, such as adding a warning to the beginning of the subjet line if the message appears suspicious.

Filtering on senders might look like this:

:0
* ^From.*nemesis@aol.com
/dev/null

:0:
* ^From.*bugfarm.org
bugfarm

Here's one the drops all messages with twelve or more recipients into a file named "SPAM":

:0:
* ^(To|CC):.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*
SPAM

This recipe puts messages with more then 10,000 characters in a file named "bigmsgs" for later perusal:

:0
* > 10000
bigmsgs

A rule like this would throw all incoming email into a file named hold4me. You might put a "catch all" rule filter like this at the bottom of all the others.

:0:
hold4later

Setting up the user's .forward file is a little tricky. Make the syntax look just like exactly this and make sure you include the double quotes. Just replace "shs" with your user's username:

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

Also make sure your .forward files are set to 600 (rw-------) permissions or sendmail is likely to ignore them.

Procmail is surprisingly modest in its use of system resources and is as good as your ability to characterize the mail that you receive.

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Comments

Ugh, procmail.

Procmail is so 1990s. :)

Install an IMAP server, like Cyrus, that supports SIEVE. SIEVE is more secure and simpler. It also works well for users who do not have a home directory (or even exist via the systems NSS).
| reply
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace