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 http://www.procmail.org and compile it yourself.
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.
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:
:0 *
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:
:0 * Mail/
This delivers your mail into a maildir-formatted directory ($HOME/Mail by default). If you prefer standard mailbox format (mbox), you can use this:
:0 * mbox
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.
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 (
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.
procmailex manpage
procmailrc manpage
egrep manpage
Adam Garside