Mandriva

Mail The Unix Way - Procmail

Summary:

  • Basics
  • Setting Basic Variables
  • Writing Recipes
  • The Sky Is The Limit
Section index - KB index

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 =
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

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
    : 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

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
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:
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:<br> ~* ^TO_&#123;your mail address&#125;<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> $&#123;MONTHFOLDER&#125;/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
is best for this purpose. Have a look at 'Procmail's' log-file to see, what actions were taken on which messages.

section index

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 :-)

section index

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.

KB - Mail The Unix Way - Procmail
Version 1.3 last modified by AdminWiki on 22/03/2004 at 09:44

 


en

RSS

Creator: AdminWiki on 2004/03/22 09:44
(c) Mandriva 2007
18888888