Mail The Unix Way - Procmail
Summary:
- Basics
- Setting Basic Variables
- Writing Recipes
- The Sky Is The Limit
Basics
'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 =
'Procmail' comes with a whole slew of documentation. Start with
man procmailex
man procmailrc
'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.
Setting Basic Variables
Start your '.procmailrc' by setting some variables:
PATH=/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin<br> MAILDIR=$HOME/Mail<br> DEFAULT=$MAILDIR/inbox<br> PMDIR=$HOME/tmp/procmail<br> LOGFILE=$PMDIR/log
-
PATH
-
MAILDIR
-
DEFAULT
-
PMDIR
LOGFILE
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'.
Writing Recipes
A simple recipe may look like this:
:0:<br> 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:<br> ~* ^From.*alice<br> 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
-
* Fromalice
-
* From*alice
-
* ^From.alice
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:<br> ~* ^TO_{your mail address}<br> 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:<br> ~* ^TO_newbie<br> 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)<br> <br> :0 Wic<br> ~* ? test 1.1 -d $MONTHFOLDER<br> | mkdir $MONTHFOLDER<br> <br> :0:<br> ~* meeting<br> ${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 like
mutt
The Sky Is The Limit
'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 :-)
Related Resources:
Procmail homepage
Filtering Mail FAQ
Procmail FAQ
Procmail Quick Start
man procmailex
Revision / Modified: July 10, 2001
Author: Tom Berger
Legal: This page is covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB and Mandrakesoft.
Version 1.3 last modified by AdminWiki on 22/03/2004 at 09:44
Document data
- Lost account?
- Join the community, be part of the Club: it's free!
- Get the PWP Download Subscription!