'Procmail' is the standard mail processing program on
Mandrake Linux when using the Postfix MTA.
Although it is the most powerful application for this
kind of tasks, its configuration syntax can be quite daunting. If you consider
using an other program for filtering your mail, you have to adapt the mailbox_command
= parameter in '/etc/postfix/main.cf' and point it to your mail processor.
'Procmail' comes with a whole slew of documentation.
Start with man procmailex which contains examples for the 'procmailrc'
file. man procmailrc lists the allowed commands.
'Procmail' is controlled by a file called '.procmailrc'
in each user's home directory. This file contains the so-called 'recipes'.
It works like this:
- Each recipe searches for one or more specified characteristics
on each incoming mail.
- If a message matches the specified criteria in
a recipe, the filter or command listed in the recipe is applied to the message.
If not, the message is passed on unchanged to the next recipe.
- A matched message may either be removed from
the incoming mail queue or be processed further by other recipes.
- If an incoming message doesn't match any recipe,
it will be delivered unchanged into the default mailbox.
section index top
Start your '.procmailrc' by setting some variables:
PATH=/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin
MAILDIR=$HOME/Mail
DEFAULT=$MAILDIR/inbox
PMDIR=$HOME/tmp/procmail
LOGFILE=$PMDIR/log
PATH : Since you may run programs from
.procmailrc, tell it where it may find them.
MAILDIR : The name of the directory where
your mail folders or files are or should be.
DEFAULT : The name of the directory or
file where all mail which doesn't match a recipe will be stored in (the 'inbox').
PMDIR and LOGFILE : Where
to put 'Procmail's' logging file. This file is very useful for examining
how your recipes work or why they do not work.
Make sure all directories you have specified exist.
If you do not wish to do any filtering, you can now
save this file. All incoming mail will be moved to the default mail box you've
specified. Just point your preferred mail agent to this file as your 'inbox'.
section index top
A simple recipe may look like this:
:0:
inbox
The first line tells 'Procmail' that this is a new recipe.
The second tells it what to do ("move to folder inbox"). This recipe will
move all mail to the folder "inbox". Now that's pretty dumb since the mail
would go there anyway if the DEFAULT variable is set to this directory.
'Procmail' allows you to define conditions upon which
an action will be taken. Conditions are marked with an *:
:0:
* ^From.*alice
wonderland
This tells 'Procmail':
- This is a new recipe.
- Look for this condition (*) in the header of the
message (default setting): A line that starts with (^) "From" followed by
zero or more instances of any character (.*) and the string "alice" (by default
not case-sensitive, i.e. "Alice" would match, too).
- If the condition is matched, move this mail to the
mail file "wonderland" in $MAILDIR
You see from this example that 'Procmail' allows you
to use regular expressions (^, ., *). Why do you need them here? Because
a condition like:
* Fromalice wouldn't work. This would
look for a string "Fromalice" in the header of the message. The "From" and
the sender's name must be separated.
* From*alice would also match a subject
like "This stems from malice". You need to tell 'Procmail' that you expect
the "From" at the beginning of a header line (^). To make the rule
even tighter, you also tell 'procmail' that this string "From" must be followed
by exactly one character (.), usually that's ':'.
* ^From.alice would be bad because the
matching line would have to look like this "From:Alice [etc]". "From: Alice
[etc]" wouldn't match (more than one character between "From" and "Alice")
and "From:<alice [etc]" neither (also more than one character).
This goes to show that writing rules can get a bit complicated
;-).
This way you can filter mail by any header field like
"From", "Subject", "Organization" etc. by adding one recipe after the other
to '~/.procmailrc'.
The handling of the "To" field however is different,
since it is usually the most important field for filtering. First of all
this field has an expression of its own: "^TO_". "^TO_" tells 'Procmail':
"look for the following expression in these fields: To:, Cc:, Resent-To:,
etc". You do not need to fiddle about with regular expression, in this case,
'Procmail' does this on its own.
This is quite handy since it allows you to put all messages which are addressed
or Cc'ed to you in a special folder. Put something like
:0:
* ^TO_[your mail address]
inbox
at the beginning of your list of recipes and
you will ensure that all messages directly written to you are moved to the
Inbox, regardless of subject or sender. Why is this handy? Let's say you
reply to a mailing list message by writing to its author directly. He replies
and Cc's the reply to the group. Without your leading 'personal' recipe,
this reply will most likely be put into the folder you have set up for the
mailing list.
The ^TO_ macro is especially useful for handling mailing lists:
:0:
* ^TO_newbie
newbie
puts all messages addressed to "newbie[...]" to a folder
called "newbie".
You can also execute shell commands from your '.procmailrc'.
Have a look at this example from man procmailex :
MONTHFOLDER=$(date +%y-%m)
:0 Wic
* ? test ! -d $MONTHFOLDER
| mkdir $MONTHFOLDER
:0:
* meeting
${MONTHFOLDER}/meeting
This recipe relies on a variable called MONTHFOLDER
which just stores a certain date format (year - month). The first part of
the recipe tests if there is a directory named after the contents of the
variable MONTHFOLDER. If not, it creates one. The second part moves all messages
about 'meeting' to a mail file 'meeting' in the current MONTHFOLDER.
You see? This sorts your mail automatically into folders named after the current
month. At the beginning of every new month, a new folder is created.
I advise you strongly to test your rules internally
by sending local mail which matches the specified criteria to your own account.
A 'simple' MUA likemutt is best for this purpose. Have a look
at 'Procmail's' log-file to see, what actions were taken on which messages.
section index top
'Procmail' can not only filter mail, but also copy it,
forward it, bounce it, create auto-replies and 'away' messages, act as a
spam filter, sort mail according to date, size and so on. Recipes can be
nested, you can change the default settings and make 'Procmail' searching
for matching strings in the emails body, strip HTML attachments or VCards
and weed out duplicate mail.
You can spend quite some time configuring it ;-). If
you want to get an impression how vast this is, get Jari's pm-tips. Be
forewarned though, this a 650 KB text file :-)
section index top
|