Procmail


Introduction

This guide will tell you some of what procmail is capable of doing for you, and point out some specific issues with using procmail. It was originally written whilst I had an account at halcyon.com, and may be referenced as A Guide to using procmail at Halcyon.

What is procmail?

Procmail is a mail processor. You can use it to filter mail or to autoreply to certain messages. You can use it regardless of whether you use a unix shell mail reader such as pine or a mail client such as Eudora. Procmail is only one of a group of programs and utilities which can work together to manage, filter and generally process e-mail.

 

Is procmail right for me?

Procmail is a serious unix hack. On the other hand, it does a pretty good job. If you don't know a thing about coding, you probably don't want to try to set up procmail.

If you know how to log into the unix shell, use pico or vi, ls and chmod you can probably set up procmail and use a simple .procmailrc (procmail rules) file. You may get that file from someone else; people are starting to share the responsibility of maintaining procmail rules files.

 

Using procmail

A couple of different things need to be taken care of in order for you to use procmail to filter your incoming mail. Many parts of the following recipes are hyperlinked to relevant portions of the man pages.

 

The steps involved

In order to use procmail you will need to:

  1. Find or create a .procmailrc rules file.
  2. Modify or create a .forward file so that your incoming mail is automatically processed.

A simple .procmailrc

A very simple .procmailrc might look very similar to the following:

 

:0
* ^FROM.*cyberpromo
/dev/null

:0
* ^Subject:.*surfing
./mail/surfing


What this file does is to send anything coming from the cyberpromo domain to the Big Bit Bucket in the Sky, and collects everything with "surfing" in the subject line into a mail folder in the pine mail subdirectory.

Analysis of a .procmailrc file

This is similar to what my .procmailrc file really looks like.

I wanted to accomplish several things. There were certain domains I wanted to suppress. I use an alias for my account when I post to usenet, and in my web client software. I wanted to trap things sent to that address, I wanted to do a little more "handshaking" with the sender to make sure that there really was somebody there and not some 'bot scanning usenet postings for addresses to spam. My overriding goal was to reduce the volume of stuff that needed to be downloaded to Eudora.

The complete uncommented version of this file is available here.

First off, I don't know exactly why but with my account at Halcyon anyway I couldn't pipe outside of the procmail stream itself without inserting the following line at the beginning of my .procmailrc:

SHELL=/bin/csh


It doesn't seem to hurt to do this as a superstitious practice most anywhere.

As I mentioned, I use an alias "fredm3047" in my web browser and when posting to usenet. I call this alias my "cookie". I wanted to catch anything addressed to the cookie and do a number of things to it.

The rule below looks for stuff addressed to "fredm3047" that doesn't have a special keyword, in this case "I-ACK" somewhere on the subject line.

 

:0
* ^TOfredm3047
* !^Subject:.*I-ACK
{


If they send something to this address without the special keyword, I want to send them back an automatic reply telling them what the special keyword is. The text of the autoreply comes from the file cookie-note.txt.

 

    :0 h c w
    * !^FROM_DAEMON
    * !^X-Loop: m3047@halcyon.com
    | ( formail -r -A"X-Loop: m3047@halcyon.com"    \
                   -I"From: fredm3047@halcyon.com" ;\
        cat cookie-note.txt ) | $SENDMAIL -oi -t


My experience is that most of the e-mail I get at this alias is junk. Nonetheless on the offchance that it's something I care about, I want Eudora to fetch the message but with all but the first two lines of the body of the message deleted.

If I want the message, then I can reply personally and request it.

 

    :0 f b w
    | head -2
}



I subscribe to a number of mailing lists. These usually don't specify my address anywhere in the To: information, a trait shared with a lot of junk mail. But I do know the domains where they come from or the name of the list they're addressed to. So I look for that information and insert a gratuitous header line into the message that identifies it as a message from a known mailing list.

 

:0 f
* ^To:.*MEME
| formail -A"X-Mail-List: MEME"

:0 f
* ^TOwednet|^FROMwednet
| formail -A"X-Mail-List: WEDNET"

:0 f
* ^To.*slime
| formail -A"X-Mail-List: SLIME"


At this point I'm ready to filter out junk mail to my "real" address. Anything not explicitly addressed to me that hasn't been tagged as coming from a mailing list is deemed to be junk.

 

:0
* !^TO.*m3047
* !^X-Mail-List:
{


I keep a copy in a file on my ISP's server. This file, because it's in the ./mail subdirectory of my account, shows up as a folder in pine.

 

    :0 c:
    ./mail/junk-mail


I go ahead and let Eudora download the headers, but before I do I add a header line that identifies it as junk mail, and I replace the body of the message with the single word "junk".

 

    :0 f h w
    | formail -A"X-Junk-Mail: Yes"

    :0 f b w
    | echo "junk"
}



Setting your .forward file

In order for procmail to be called, you have to create a .forward file. So-called because you can use it to forward your e-mail to a different account; so for instance if you have multiple e-mail accounts, you don't have to log into them all to check your mail, you can have all of the mail from various accounts forwarded to a single one.

However you can also specify a program to be called when mail arrives. That's what we're doing here. Substitute your account name for your-account in your-account@halcyon.com.

Use pico or vi (or your favorite editor) to create a file named .forward in your login directory. Typically this file will have one line:

 

"|exec /usr/local/bin/procmail #your-account@halcyon.com"


Make sure that the file is world-readable, but is only writeable by you:

 

[coho]> chmod u=rw,go=r .forward
[coho]> ls -l .forward
-rw-r--r--  1 m3047          39 Oct  2 08:02 .forward


Documentation

Era is too modest, and doesn't want to be recognized as an "authoritative" resource; but I see the web logs, and I read the e-mails. ;-) This is Era's procmail site in Finland.

There are several sources of documentation available. The Filtering Mail FAQ is a overview of mail filtering that covers other methods as well as procmail.

Panix's E-Spam page might be informative, and has a link to a filter they supply to their own customers.


On your system itself, documentation for procmail may be available through the localman command. So, if man doesn't work, try localman. The specific man pages that you'll find most interesting and edifying are

[coho]> localman procmail
[coho]> localman procmailrc
[coho]> localman procmailex
[coho]> localman formail


Troubleshooting

You may want to review the procmailex manual especially, as it includes several recipes for saving off your mail while you're debugging your .procmailrc file.

You may also find inserting the following two directives at the beginning of your .procmailrc to be useful:

 

LOGFILE=procmail.log
VERBOSE=yes


Both of them are documented in the procmailrc manual.


This document is provided solely as a guide and is not guaranteed to be fit for any particular purpose.

USE IT AT YOUR OWN RISK


Author: Fred Morris

Contact: fred@unixgeeks.org

last updated 17-Jul-2001

Original location: http://www.inwa.net/~m3047/procmail/procmail-guide.html