Mail Filtering with Linux
With Linux it's easy to filter the vast hoards of mail that might come your way. I receive nearly a thousand email messages a day from many mailing lists, and I cannot read them all. But by using procmail, I can filter all incoming mail into various folders by subject and by the relevant mailing list, and then I can delete topic threads that don't interest me yet follow threads that do. In this way, I can know what's generally going on without wasting time reading ``noise'' that isn't germane to my research. Frequently, I find other topics for future research and learn things I wouldn't have thought of by using this method.
Running Procmail
The main procmail site is
ftp://ftp.informatik.rwth-aachen.de/pub/packages/procmail/
The chances are that procmail was included on your Linux CD-ROM and
you may have already installed it. For example, if you're using the
popular Red Hat Linux distribution and you included it during
installation, all you need is create a file called
.procmailrc in your home directory. You can check to see
if you have procmail installed by typing
type -path procmail
at the user prompt. Depending on how it's installed on your
system, you may need a .forward file. A simple example of your
.procmailrc file could look something like this:
PATH=/bin:/usr/bin:/usr/sbin # path so pm can find programs MAILDIR=/home/yourusername/mail # Pine uses this directory LOGFILE=\$HOME/procmail/log # Better keep a log file %$ SENDMAIL=/usr/sbin/sendmail # Here's where sendmail is FORMAIL=/usr/bin/formail # Formail formats mail #Create a backup file :0 c backup #This keeps the backups manageable :0 ic | cd backup && rm -f dummy `ls -t msg.* | sed -e 1,32d` #File mailing lists into folders #Here's the Red Hat List :0 * ^Resent-From: redhat-list@redhat.com /home/yourusername/inbox/RedHatList #Here's the Debian User List :0 * ^Resent-From: debian-user@lists.debian.org /home/yourusername/inbox/DebianUserList #Here's the Kernel Panic User Group List :0 * ^Reply-To: kplug-list@ultraviolet.org /home/yourusername/inbox/KPLUG
About the Recipies
This little recipe is a simple example. You'll find more examples in your man pages for ``procmail'', ``procmailex'', and ``procmailsc''. You should also look at the man pages for ``formail'' and ``sendmail''.
Basically, you only have three types of statements in your .procmailrc file: variables, comments, and the recipes themselves. The variables in the above example are FORMAIL and SENDMAIL and MAILDIR. Thus, you could direct formail in one of your recipes by using this recipe:
#Send partiers directions to my house #when subject=''party directions''! :0 * ^Subject:.*party directions |($FORMAIL -r cat $HOME/directions.txt) | $SENDMAIL -oi -tto send someone the contents of ``directions.txt'' (directions to your house for a racaus party!). Using variables in this way can make life much simpler when you have executables strewn all over your system and you get tired of updating your PATH statement all the time.
The notes are rather self-explanatory. They can go on their own line before the recipe or on the same line as a variable, but I would not mix them up in the recipe itself. That is, don't do something like this:
#This sample recipe deletes anything from ``Spamguy'' :0 #This comment shouldn't be here * ^From:.*Spamguy /dev/null
Notice that the first part of the recipe is the colon, followed by a
zero, not the letter O. You can pass other arguments on the same line
that will do convenient things (see the procmailrc man page). If
there is a second colon, it is to designate a local lockfile for this
single recipe only. (See man procmail about lockfiles.)
The second part of the recipe is the condition and usually starts with an asterisk. If this condition, or set of conditions, is met, then the subsequent part of the recipe, the action, will be executed. The condition may consist of several lines so that if any one of them is met, the action is executed on the mail. Or only if all of them are met. Or if any combination of them is met. So, here's the Kernel Panic User Group mailing list recipe again.
:0 * ^Reply-To:.*kplug-list * ^Subject:.*procmail /home/yourusername/inbox/procmail
In this case, Procmail will search mail headers for lines that start with ``Reply-To:'' and have ``kplug-list'' in them as well as ``procmail'' in the subject line. Then Procmail will simply file the message in /home/yourusername/inbox in a file called procmail. Then you designate that incoming folder as one of your ``inboxes'' for your mail reader of choice.
You could also create a little script consisting of several instructions in this next recipe. For example,
:0
* ^Resent-From:.*redhat-list@redhat.com
* ^Subject:.*Procmail
* ^From:.*FooBar
{
:0 c
! root@foobar.com
:0
/home/yourusername/messages/procmail
}
The recipe above performs two tasks. If it finds a message from the redhat-list with the subject of Procmail from FooBar, it will copy the message to the system administrator at foobar.com and will also store the message in my home directory in a folder called ``messages'' in a file called ``procmail.'' Groups of instructions, much more complicated than this, can be built according your needs and tastes.
Once again, see man procmailex, man procmailrc, and man procmail for specifics on symbol meanings I haven't discussed here.
Other Uses for Procmail
Procmail may also be used for many other purposes. You can strip unwanted attachments, such as Mozilla greeting cards or Microsoft Exchange MIME attachments, and you can run your mail through various custom filters of your own devising, which you can call from Procmail recipes. You can set up auto-responders for PGP keys, vacation messages, job status, and yes, directions to your house party. You can even perform scripts on your mail too.
Perhaps one of the most popular uses is filtering your email for SPAM (unsolicited bulk email) that is not caught by sendmail filters. Several packages of recipes have been written which take various approaches to filtering SPAM out of your incoming mail by using Procmail. One of my favorite recipe collections is called Junkfilter and can be found at http://www.pobox.com/~gsutter/junkfilter. Another resource you'll want to see are Jari Aalto's Procmail pages at ftp://cs.uta.fi/pub/ssjaaa/pm-tips.html. Alan Stebbens collects some good recipes at http://reality.sgi.com/aks/mail/procmail.
You'll also want to check out the Procmail FAQ at http://www.helsinki.fi/~reriksso/procmail/mini-faq.html and the Mail Filtering FAQ at http://www.faqs.org/faqs/mail/filtering-faq/. From these pages you can find quite a few other Procmail resources to help you get going. There's also a mailing list at procmail@informatik.rwth-aachen.de. You can be subscribed by sending your email address to procmail-request at the above domain with ``subscribe you@foobar.com'' in the subject (use your own email address, obviously).