Newbie dot Org HomePage
Visit one of our web buddies
Xxaxx's Xperimints #1
Procmail

First let's look at a sample .procmailrc file:

# .procmailrc
# routes incoming mail to appropriate mailboxes
PATH=/usr/bin:/usr/local/bin
MAILDIR=$HOME/mail
LOGFILE=/dev/null
#LOGFILE=$MAILDIR/procmail.log
SHELL=/bin/sh
SENDMAIL=/usr/bin/sendmail
 
# Put mail from newbie-list mailing list into mailbox newbie
:0:
   * ^(From|Cc|To).*newbie-list
   newbie
 


# .procmailrc
This line is not required. It is a comment line used to identify the file. The # keeps the rest of the line from being interpreted as code and is considered commentts.

 


# routes incoming ...
This line is not required. It is a comment line used to remind us of what the heck the script is supposed to be accomplishing. Just because you know what you are doing today does not mean you will remember in the future. Go ahead and make comments when you can.

 


PATH=/usr/bin: /usr/local/bin
This tells Procmail where to look for other programs. /usr/local/bin is a very common place for other programs to be found. /bin and /usr/bin are also used for programs that might be run. By telling procmail where to find other programs it can run them from the script.

Directories are separated by colons.

Also you will notice this line takes the form variable-name=value.

 


MAILDIR=$HOME/mail
MAILDIR is the location of your mailboxes. If Procmail is going to sort your mail it will need to know where this mail box is located. $HOME stands for your home directory. This is a variable that seems to be predefined on most systems. You will have to verify that it exists on your actual website. It may or may not. Mostly it will. If it doesn't see if you can find out from your ISP what name they are using.

This means that your mailbox is a subdirectory of your main home directory.

 


LOGFILE=/dev/null
This specifies the name of a file to use as a usage log. Procmail will write any diagnostic or error messages into this file during its activity. In this example, logging is being send to /dev/null, which is special system black-hole for dumping unwanted data. It is like the "round" filing cabinet. I.E. no logs will be kept by this .procmailrc file. Logs can be useful for tracking down errors when creating new new recipes. After the recipe is proven to be working you don't need the log file. Hence the use of /dev/null

 


#LOGFILE=$MAILDIR/procmail.log
The # is used to comment out the alternate logfile used during debugging. When debugging is being done the "LOGFILE=/dev/null" is commented out with a # and this line is uncommented out by removing the # to become.

LOGFILE=$MAILDIR/procmail.log

This will create a file in the $HOME/mail subdirectory called procmail.log which will contain usage data and error messages.

 


SHELL=/bin/sh
This defines a shell, or operating environment, for Procmail to run other commands in.

 


SENDMAIL=/usr/bin/sendmail
This defines the location of the sendmail program. This is required for forwarding and autoresponding to incoming email.

 


 
# Put mail from ...
This line is not required. It is a comment line used to remind us of what the heck the script is supposed to be accomplishing. Just because you know what you are doing today does not mean you will remember in the future. Go ahead and make comments when you can.

 


:0:
Recipes have the following format:

:0 [flags] [: [lock-file] ]
zero or more conditions
one action line

The last colon means use a lockfile. Why use a 'lockfile'? Well, suppose two messages came in at about the same time both addresses to 'newbie'. It's very possible that the mail system would run two copies of Procmail, and each would try to write its message to your 'newbie' folder! By using a lockfile, the first Procmail that gets run will 'lock' the folder so only it can write to it; any other Procmail trying to write to that folder will have to wait until the first is finished. Using lockfiles may slow down your mail delivery ever so slightly, but it's better than mangled mail.

 


* ^(From|Cc|To).*newbie-list
The condition tells Procmail what to look for in a mail message. They begin with a '*', and the rest is a pattern to look for. If part of the message matches this pattern, then Procmail will apply the action defined by the next line. The pattern is called a regular expression, and takes some explaining. See the associated page that covers regular expression.

Procmail has some handy built-in shorthands which expand into complicated regular expressions. (These are not true macros, they expand only when used as part of a regular expression.)

^TO -- This covers most headers which can carry your address in them, such as To:, Apparently-To:, Cc:, Resent-To:, etc. You should perhaps use ^TO_ instead.
Note that there is no corresponding ^FROM macro.

^TO_ This is a better bounded version of ^TO which unfortunately doesn't exist in older versions of Procmail (it was introduced in version 3.11pre4). Unless backwards compatibility is an issue, you should probably prefer ^TO_ over ^TO.

^FROM_MAILER -- This should cover most messages from mailer programs (bounces etc.).

^FROM_DAEMON -- This is an expanded version of ^FROM_MAILER which covers a broader range of machine-generated messages, notably including most mailing lists and other (benign) bulk e-mail.

Whenever creating an autoresponder you will need to filter for stuff from a daemon or mailer so that you can avoid creating a ringing of an email between two autoresponders.

 


newbie
The action line in this case is simple: it's just 'newbie,' the name of the folder to put the mail into. The action could also be an address to forward the mail to, or a program to start, or even a block of commands. We'll see more complex examples later.