Filtering Incoming Email with Procmail

When mail is coming in to your server, you can filter this mail and determine where the mail needs to be stored. Procmail is the MDA used for this purpose. It can sort e-mail automatically, forward it to other recipients, or delete it automatically according to some criteria the user has specified. Procmail can be useful if, for example, you want to get rid of some e-mail before it even arrives in your mailbox. The last option is particularly interesting, because it helps you remove spam automatically.

To use Procmail, it has to be called from the Postfix file. This is usually done with the following line, which is added to the Postfix environment by default:

mailbox_command = /usr/bin/procmail

When it is activated, you can use Procmail to set up automatic e-mail filtering and redistribution. By default, e-mail is delivered to the user mailboxes in /var/spool/mail/.You can change this behavior by creating a .procmail file in the home directory of the individual user. Listing 16-8 shows an example of automatic mail filtering performed by Procmail.

Listing 16-8. Example of .procmail Mail Filtering File

PATH=/bin:/usr/bin MAILDIR=$HOME/Mail LOGFILE=$MAILDIR/mail.log

* AFrom.*somedomain $MAILDIR/Somedomain

:0 ASubject.*viagra $MAILDIR/spam


In this example, you can see three rules that are applied, after some specific variables have been set that specify where mail binaries can be found. The first rule specifies that data in the From field will be compared to *somedomain. If there is a match, the mail is automatically forwarded to the Somedomain folder in the mailbox of the user. Next, you can see that all messages that have viagra in the message subject line are forwarded automatically to the spam folder. This is a primitive way of handling spam. Last, all other mail (matching the * criterion) is placed in the folder Inbox.

Was this article helpful?

0 0

Post a comment