Running Linux, 5th Edition - Matthias Kalle Dalheimer [440]
Preparing Procmail for Use
Procmail comes with most modern Linux systems nowadays, but should it not be available for your system, then you should have a look at http://www.procmail.org. At this site you will also find a large collection of sample recipes.
When you have ensured that Procmail is on your system, it is time to check if it is invoked by your MUA. The easiest way to do so is to place the following .procmailrc file in your home directory, and send yourself an email.
SHELL=/bin/sh
MAILDIR=${HOME}/Mail
LOGFILE=${MAILDIR}/procmail.log
LOG="--- Logging ${LOGFILE} for ${LOGNAME}, "
If the ~/Mail directory does not exist, then you need to create it for this script to work. If you store your email elsewhere, replace ${HOME}/Mail with the alternative location. Also please check that /bin/sh exists (it's quite likely that it does); otherwise, adapt the script.
If Procmail is invoked by default, then the file just shown should give you ~/Mail/procmail.log, with content similar to the following:
--- Logging /home/test/Mail/procmail.log for test, From blackie@blackie.dk
Fri Mar 18 12:25:23 2005
Subject: Fri Mar 18 12:25:22 CET 2005
Folder: /var/spool/mail/test
If this file didn't come into existence by sending yourself an email, don't panic. All you need to do is to add the following line to the ~/.forward file:
|IFS=' ' && exec /usr/bin/procmail || exit 75 #myid
Replace /usr/bin/procmail with the path to your system's Procmail binary, and replace myid with your login name. (This part is necessary to avoid problems with MUAs trying to optimize mail delivery.)
Now send yourself a mail again, and check if it works this time. If you still do not see the file, then it might be a result of a system that is too closed. Check that the .procmailrc and .forward files are readable by others, and perhaps only writable by yourself. Possibly you also need to add the x flag to the attributes of your home directory, which you may do with this command:
chmod go+x ~/
If things still do not work, then it is time to panic—or at least consult the vendor of your Linux system.
Setting up a sandbox
Are you ready to lose your email while playing with Procmail ? If not, then it might be a good idea to create a sandbox for your tests. To do so, create a Test directory, and copy your .procmailrc file into that directory and name it proctest.rc. Now edit this file instead of your real .procmailrc file when testing things. In the Test directory, create this shell script:
#!/bin/sh
#The executable file named "proctest"
#
# You need a test directory.
TESTDIR=~/Test
if [ ! -d ${TESTDIR} ] ; then
echo "Directory ${TESTDIR} does not exist; First create it"
exit 0
fi
procmail ${TESTDIR}/proctest.rc < mail.msg
You may wish to adjust the LOGFILE line of the proctest.rc file so it doesn't write to your existing logfile, but instead simply writes to a logfile in the Test subdirectory. You may also want to add the following line to get improved debugging output from Procmail:
VERBOSE=yes
LOGABSTRACT=all
Finally you are ready to run the tests, which you do by placing a mail message in the file mail.msg, and running the script proctest. Most email programs allow you to save just one email to a file. Alternatively, send yourself an email, and copy it out of your /var/spool/mail/your-login file.
Recipe Syntax
With all the preparation done, we may now start looking at recipes. Recipes all follow this style:
:0 [flags] [ : [locallockfile] ]
<0 or more conditions (one per line)>
Conditions start with a leading *. Everything after that character is passed on to the internal egrep literally, except for leading and trailing whitespace. The action line may take several forms: If it starts with a !, then the rest of the line is considered an email address to forward to. If it starts with a |, then the rest of the line is considered a shell command to be executed.