Procmail talk
Andrew C. Bulhak <acb@dev.null.org>
What is Procmail?
Procmail is a tool which runs under UNIX for automatically
processing mail by matching against headers and body text with
regular expressions.
What can Procmail do?
Procmail can be used for a number of tasks, which include:
- Sorting incoming mail into folders.
- Deleting junk mail
- Performing local mail delivery
From where can Procmail be obtained?
The procmail source resides at
ftp://ftp.informatik.rwth-aachen.de/pub/packages/procmail/procmail.tar.gz.
Many Linux distributions provide a precompiled procmail package.
How can Procmail be used?
From the user's .forward file
Most mail delivery agents, such as sendmail, can allow users
to specify programs to pipe incoming mail through. This can be used to
invoke procmail. To do this, the user's .forward file
should read something such as:
"|IFS=' '&&exec /usr/local/bin/procmail -f-||exit 75 #YOURUSERNAME"
From a sendmail alias.
Mail aliases (kept in /etc/aliases) behave much like .forward
files. It is thus possible to create a mail alias which will pipe incoming
mail to procmail, like so:
information: |/usr/local/bin/procmail -m /etc/procmailrcs/info
This can be useful for writing "infobots" and the like.
As the site's local mail delivery agent.
procmail can be configured to take the place of a site's local
mail delivery agent. This can be done with sendmail 8.8 (I think) by specifying the local_procmail feature in the sendmail.mc file.
This has the further advantage of allowing anything after a '+' in the local
part of the email address to be passed as an argument to procmail.
Procmail recipes
- The rules by which mail is handled.
- Procmail first looks for recipes in /etc/procmailrc (if any). After that, it reads recipes from $HOME/.procmailrc
Procmail recipes
- A recipe is one rule for peforming an action.
- A recipe consists of conditions and actions.
- A condition is (in most cases) an egrep-style regular
expression,
which is matched against the mail headers (unless specified otherwise).
If the message matches, the condition is met.
- Condition lines begin with a '*'.
If there is more than one condition, they are anded.
If there are no conditions specified for a recipe, the condition is
deemed to be satisfied automatically.
- Weighted scoring is possible. (In this case conditions add to a
score if satisfied, and messages are matched by final score.)
- An action is a line specifying what to do with matching mail.
Valid actions include:
- Appending to a file. In this case, the action line is a file path
(such as Mail/melblinux).
- Forwarding the mail to another address. The action line consists
of a '!' followed by the address.
(i.e., ! joe@foo.bar.com)
- Piping the mail through a program. The action line consists of
a '|' followed by the comand line.
(i.e., "| bin/handle_mail.pl")
- A nested block; this consists of recipes enclosed in curly brackets.
Iff the conditions are met, the recipes in the block are invoked.
Formail
- Supplied with procmail.
- Formats mail messages using command-line arguments.
- Extracting/changing/appending headers
- Splitting digests
- From escaping.
Mail logging and mailstat
Procmail can be configured to log all mail processed to a file whose
path is set in the LOGFILE variable.
There is a utility program named mailstat which, when called with
the name of this file, reads it, prints a table of statistics and
deletes it. This is good for informing the user about where mail has been
delivered.
My .procmailrc
LOGFILE=$HOME/.maillog
:0:
* ^Sender: owner-flat-earth
Mail/f-e
:0:mc.lock
* ^To:.*mornington-crescent@mono.org
Mail/mornington-crescent
:0:mc.lock
* ^To:.*mornington-crescent@mono.city.ac.uk
Mail/mornington-crescent
:0:s-t.lock
* ^To:.*silent-tristero@world.std.com
Mail/s-t
:0:eta.lock
* ^Sender: eta-request@eris.very.net
Mail/eta
:0:test.lock
* ^Subject: PROCMAIL_TEST
Mail/test
#:0:spam.lock
# * ^To:.*@(aol\.com|juno\.com|interramp\.com|cyberpromo\.com|Recipient\ list\ suppressed)$
#Mail/spam
# :0:
# * ^From:.*MAILER-DAEMON@cs.monash.edu.au
# /dev/null