How to enable Procmail in Red Hat Linux 7.0

By Phil Jones (pjls16812 at blueyonder.co.uk)

Index


Intro

This describes Procmail basics in Red Hat Linux 7.0. Procmail provides a simple way to sort mail automatically after it arrives. Procmail can also do many other things but we will just look at sorting incoming mail.

These instructions are for getting Procmail to work. I spent an entire day trying to get Procmail to do something, anything. Eventually I found that Procmail is, contrary to first appearances, simple to use for simple tasks.


Setup

The good news is Sendmail supplied with Red Hat Linux 7.0 is Procmail aware. All you have to do is write your Procmail configuration file and it will start working straight away.

The basic Procmail file

Create a file in your home directory called .procmailrc, like this:
touch ~/.procmailrc
Edit the file so it looks like this:
# Turn on detailed logging so we can see what happens.
VERBOSE=on
LOGFILE=/home/phil/procmail_log

# Move mail in the new mail file '/var/spool/mail/phil' to the 
# '/home/phil/Mail/testing' mail file, if the subject is 'testing'. 
# If the 'testing' mail file doesn't exist, it is created
# automatically.
:0:
* ^SUBJECT.*testing
/home/phil/Mail/testing
This will move new mail to the 'testing' mail file, if the subject is 'testing'. Mail files are plain text files containing mail text. Remember to substitute your username for 'phil', which is my username on my Red Hat Linux 7.0 system.

The lines beginning with '#' are comments and they are ignored. The line starting ':0' indicates the beginning of a rule, or 'recipe'. The second colon makes Procmail create a lock file. The lock file prevents other programs from writing to the incoming mail file while Procmail is working on it. This is a good thing.

The '*' indicates the beginning of a condition. The condition is written as a 'regular expression'. The condition is "look starting at the beginning of the line for 'SUBJECT', followed by zero or more of any characters, then the word 'testing', then zero or more of any characters." The condition is case insensitive.

If the condition is met, then the message is moved to the mail file '/home/phil/Mail/testing'.

Save changes and quit. The file must be in Unix text format. Procmail doesn't understand MS-DOS text format line endings.


Testing

Send yourself a mail with the subject 'testing'. You may hear your computer's hard disk whizz a bit as the mail goes into Sendmail, into your new mail file, and Procmail does its stuff. The 'testing' mail file should now be created. Have a look at the file, like this:
cat /home/phil/Mail/testing
Remember to replace 'phil' with your username. You should see something like this:
From phil  Wed May  9 12:35:36 2001
Return-Path: 
Date: Wed, 9 May 2001 12:35:36 +0100
From: Phil 
Message-Id: <200105091135.f49BZa902253@localhost>
To: phil@localhost
Subject: testing 

It worked!
Notice how Procmail has picked up the word 'Subject' at the beginning of one of the lines, followed by zero or more of any characters, then the word 'testing', then zero or more of any characters. Next have a look at the 'testing' mail file with your mail reader, and you should see your test message in there.

What next

For more information try these commands: man procmailrc and man procmailex.

It didn't work!

Have a look at the 'procmail_log' file which should have been created in your home directory. Also check the Sendmail log file, '/var/log/maillog'. You need to be root to read the Sendmail log file.

I did the test, it seemed to work but I got no 'You have new mail' message, scream, wail, why not?

Procmail took the mail out of the new mail file (/var/spool/mail/your_username_here) and moved it elsewhere. Examine the procmail log file and you will see it has done that. Most 'mail alert' programs watch the new mail file, and they won't do anything if Procmail puts it somewhere else. The solution (I think) is to set the 'mail alert' program (eg KBiff on KDE) to watch the Procmail destination mail file as well as the normal new mail file.

Help with regular expressions

Regular expressions are powerful but insanely cryptic and intimidating for the beginner. Fortunately, help is at hand with the wonderful Text To Reg Exp utility. It lets you build your text matching expression, step by step, in English. Brilliant. Here is an example of it in use:

[.]quit [0]reset [*]color [/]progs __ unknown !! not supported RegExp emacs : RegExp gawk : RegExp grep : RegExp perl : RegExp php : RegExp python: RegExp sed : RegExp vim : .oO() [1-2]: start to match: 1) on the line beginning 2) in any part of the line