Team LiB
Previous Section Next Section

Hack 58. Reclaim Your Email with procmail

Manage your email automatically without depending on your email client.

Email is the Internet's killer app. Everybody uses it, everybody loves iteven those who feel like their lives are controlled by it. The volume of email in our lives has increased exponentially the last few years, and I'm not referring just to spam. With email's acceptance as a mainstream communications medium, it has become increasingly important for users to manage their volume of email efficiently. Most GUI email programs provide numerous features to help manage email, including searching, filters, and spam-checking. But for some users these features aren't enough. They have found it best to augment their email client with the Swiss Army Knife of email processing, procmail. This email tool allows you to define what you do with your mail, where it goes (if it goes anywhere), and how it should be stored using a simple, albeit terse, rule-based syntax. This hack explores how to use procmail.

To get procmail, use your distribution tools to search for and install it. For instance, on Debian, use:

foo@bar:~# apt-get install procmail

If your distribution doesn't come with procmail, you can always download it from and compile it yourself.

8.5.1. Configure Your MTA to Use procmail

procmail is typically used on computers that have a Mail Transfer Agent (MTA) running, and as such, before you use procmail you must configure your MTA to use it. The majority of distributions include a default MTA that, although sometimes not installed by default, can be installed at a later time. An example of this is Debian, which includes the Exim MTA as the default server. You will need to ensure that your MTA is able to send and receive mail before you can begin configuring procmail.

Along with the many different MTAs available are many different ways to configure them to use procmail. Despite the mass of potential options, an almost foolproof method is to use a .forward file in your home directory. This file contains rules that, when followed, invoke procmail for all incoming mail. The following command enables procmail for most MTAs:

foo@bar:~$ echo '|exec /usr/bin/procmail' > ~/.forward

If your MTA complains, you might have to use just |/usr/bin/procmail instead.

8.5.2. Using procmail

procmail rules, better known as recipes, are defined in the .procmailrc file in your home directory. Although a full treatment of the recipe syntax is beyond the scope of this hack, a simple example shows the basic format. Add the following to your .procmailrc file:


This simple recipe delivers all mail addressed to you to your default system mailbox file (usually /var/mail/$USER). The :0 indicates the beginning of a rule and the * indicates that all mail should be handled by this rule. This re-creates what your MTA usually does by default. If you use the maildir format (the preferred format of the qmail MTA and the KMail client), procmail can move all mail to a directory with a recipe, such as this:


This delivers your mail into a maildir-formatted directory ($HOME/Mail by default). If you prefer standard mailbox format (mbox), you can use this:


This recipe delivers all your mail to $HOME/mbox by appending all new mail to the same file. Although these examples are trivial and do no more than your MTA, they are the most basic recipes procmail uses. These simple concepts form the basis behind more complex procmail recipes.

8.5.3. Standard procmail Usage

Now that you know how procmail works, you can use it to do some processing. Say you want to store mail from your friends Bruce ( and Steve ( in a different mailbox file or maildir directory. You can use the following recipe:


* ^From.*(|

Any mail from Bruce and Steve will be stored in the maildir-formatted directory $HOME/Mail/friends. maildir format is denoted by the trailing / in the rule; if you don't use the /, the mail will be delivered to a standard mailbox file named friends in your $HOME/Mail directory. The regular expression in the second line of the recipe conforms to the usage of egrep, so anything that egrep can use as a regular expression can be used in procmail rules. Consult the manpage for egrep for more information.

You can also use this format to separate mail sent to a mailing list into a specific directory. This is shown in the following recipe:

* ^TObugtraq

This recipe stores all mail sent to the bugtraq mailing list in a bugtraq mailbox in your $HOME directory. The second : in :0: tells procmail to use a default lock file to prevent multiple instances of procmail from trying to write to the mailbox at once. The ^TObugtraq regular expression is shorthand that expands to a more complex regular expression that catches all mail sent to an address containing a specific word (bugtraq in this case).

Now let's look at something fun. If you have festival (a text-to-speech system that is freely available on most distributions), you can have it speak to you when new mail arrives, using this recipe:

    SUBJECT=`formail -xSubject:`
    FROM=`formail -xTo:`

    :0 c

    :0 W
    | echo "Incoming mail from, ${FROM}. Subject is: ${SUBJECT}" \
      |/usr/bin/festival --tts -

The {} allow you to combine more than one rule. The formail utility that is bundled with procmail allows you to grab various headers and, in this case, put them in variables for use in the second rule. The c in :0 c indicates this rule works on a copy of the incoming message, and the message will continue on through the various rules that follow.

Other potential uses for procmail include sending all mail through Spamassassin to root out all the junk and prevent it from entering your mailboxes.

8.5.4. See Also

  • procmailex manpage

  • procmailrc manpage

  • egrep manpage

Adam Garside

    Team LiB
    Previous Section Next Section