Setting up Procmail on your FIU-SCS Account

Setting up Procmail on your FIU-SCS Account

By John Flynn / flynnj @ cs.fiu.edu / 7/26/2002

Introduction

Procmail is a tool that is designed to filter your mail. In other words, it permits you to send your Email to different locations depending on its content. This is very handy because you can use it to do things like:

and much more.

Setting up Procmail

In the past, you used to have to use a ".forward" file to use procmail. You no longer have to do this, since *ALL* email is delivered using procmail. The only file you have to worry about is your .procmailrc file, which is located in your UNIX home directory.

The first step is to create a mail directory inside your home directory, if you don't already have one. Just type, in UNIX:

cd ~
mkdir mail
chmod 700 mail

The chmod command keeps other people from poking around in your mail directory, but allows you to access it.

The second step is to actually create the file. For instance, in UNIX, type:

pico .procmailrc

(or emacs, or vi, or whatever editor you want; pico is easiest for beginners, though)

Then, paste in the following. This is the procmail header, which defines various operating parameters.

# .procmailrc begins here

PATH=$HOME/bin:/usr/bin:/usr/ucb:/bin:/usr/local/bin:.
MAILDIR=$HOME/mail      # Make sure it exists
DEFAULT=/var/mail/$LOGNAME
VERBOSE=off
LOCKFILE=/var/mail/.proclock.$LOGNAME
LOGFILE=$MAILDIR/.procmail.log

# Recipes go here
# 

# .procmailrc ends here

Procmail automatically replaces "$LOGNAME" with your username and "$HOME" with your home directory.

The LOCKFILE is used by procmail to keep two different procmail processes from accessing your mailbox at the same time, causing corruption. The LOGFILE contains a log of procmail transactions; this is handy if something is not working and you want to find out why. DEFAULT specifies the destination of all Email that does not match any recipies. Since there are no recipes yet, all your incoming email will go to DEFAULT, which is your mail spool.

Creating Recipes

A recipe is a rule that tells procmail what to do with a particular Email. Recipes must be placed after the header in your .procmailrc file, IE, after the "Recipes go here" comment in the above example. Here are some examples of useful recipes.

Let's say we have a jerk with the Email address "jerk@microsoft.com" who is sending you lots of annoying Email, and you want to stop seeing the messages. This recipe will do that:

# Begin procmail recipe
:1
^From.*jerk\@microsoft\.com.*
/dev/null
# End procmail recipe

Let's break this down and explain what it does.

First line:  :1

There is always a : on the first line of every recipe. The number (in this case 1) after the : specifies how many rules follow for this recipe. All the rules must match for the recipe to take the message.

Second line: ^From.*jerk\@microsoft\.com.*

This line will match any Email that contains the above text. Since the From header is integral to most Emails, and contains the sender's Email address, that line will match messages from "jerk@microsoft.com". The line is a regular expression, so standard UNIX regular expression syntax applies.

Third line: /dev/null

The last line in a recipe tells procmail what file to send the message to. It is called the "action line". In this case, we're sending the message to /dev/null, which is the "bit bucket", all data sent there is deleted.

Remember that jerk@microsoft.com could always change his email address, or find another ISP. This won't necessarily stop someone from harassing you. It is best to not brag about the filter; if the person doesn't know his email is being deleted, he will likely continue sending the messages and wonder why you aren't replying!

Example: Filing certain messages into folders

Here is another example of a procmail recipe; this one will take all messages from pelina@cs.fiu.edu and place them in the mail folder "pelina".

# Begin procmail recipe
:1
^From.*pelina\@cs\.fiu\.edu.*
/homes/$LOGNAME/mail/pelina
# End procmail recipe

Since $LOGNAME is replaced by your username, messages from pelina@cs.fiu.edu will be stored in the mail spool file "/homes/username/mail/pelina", which you can access from pine and/or imap.

Example: Carbon Copies

Let's say you want to do something with a message, like file it away, but still want the message to continue being processed by the remaining recipes. To do this, add a "c" after the number in the : line. For instance:

# Begin procmail recipe
:1 c
^From.*pelina\@cs\.fiu\.edu.*
/homes/$LOGNAME/mail/pelina
# End procmail recipe

The above recipe will save a CARBON COPY of all messages from pelina@cs.fiu.edu to the "pelina" mail spool file, and the message will still be delivered to your INBOX, provided there are no more recipes after this one that match that message and do something else with it.

Example: Forwarding

You can also use procmail to forward your mail. Say you get a lot of personal email at your school address from friend@yahoo.com, and you don't want it distracting you at work. You can quietly forward that to your personal email (say it's guru@foo.org) with a rule like this:

# Begin procmail recipe
:1
^From.*friend\@yahoo\.com.*
! guru@foo.org
# End procmail recipe

The "!" in the action line tells procmail to send the message to that address.

Summary

In a .procmailrc file, all recipes will be checked in order until a match is found. Therefore, if two recipes match a message and the first one doesn't have a carbon copy directive on it, the second recipe will never be processed.

There are many other tricks you can perform with procmail; you can find them, and much more detailed documentation, at http://www.procmail.org/.

If you are associated with the FIU School of Computer Science and have any questions you can also E-mail us at request@cs.fiu.edu and we will do our best to assist you.