Procmail

About Procmail

 

Table of Contents


What is Procmail?

Procmail is a free open-source program that runs on top of your regular mail client (e.g. elm, pine). USES: creating mail-servers, mailing lists, sorting incoming mail into separate folders/files (real convenient when subscribing to lists), preprocessing your mail, starting any programs upon mail arrival (e.g. to generate different chimes on your workstation for different types of mail) or selectively forwarding certain incoming mail automatically to someone. This is a basic tutorial to get you started, but with a little research on your part, you can use Procmail to do much more.

Procmail tutorials:

Bug reports should be sent to bug@procmail.org

How to Setup Procmail

1. Modify your .forward file

Incoming mail to your account will always check first for a .forward file before going to your mail directory. If one is found, then anything in the file will be executed first. If you would like for Procmail to handle your mail sorting, then add this line to your .forward file(create one if you don’t have one). If there is a line with your email address on it, comment that line out.

“|exec procmail” (with double quotes)

2. Create .procmailrc After you have added the exec command to your .forward, you need to tell Procmail what to do with your mail. This is done in the .procmailrc file(that you will create)through the use of “recipes.”

.procmailrc

The easiest way to configure procmail is to just put your recipes right in your .procmailrc file. The .procmailrc file must be located in your home directory, but other than that you may sort your mail to any directory path you choose.

The first line in the file tells procmail where its logfile is located. This is what mine looks like:

LOGFILE=$HOME/Procmail/log

So, my logfile is located in a directory called Procmail, which is in my home directory, and the file itself is called log. The next line in the .procmailrc file sets the procmail MAILDIR environment variable to the directory of where your mailboxes are stored. If you use elm and pine, then set your MAILDIR like this:

MAILDIR=$HOME/Mail

You can also add the line VERBOSE=no to minimize logging and save space.

Finally, you add your recipes.

Procmail Recipes

A Procmail recipe is a set of conditions that, if met, order the specified action to be executed. In Procmail, regular expressions are used in the conditions of a recipe to, for example, look for a pattern in the Subject header of a message.

Recipes are preceded by a :0 and have the following format:

:0 [flags] [: [locallockfile] ]
<zero or more conditions (one per line)>
<exactly one action line>

Here is a good one to use for testing:

:O:
* ^From.*username
IN.testing

Replace “username” with your username and save and exit the file. Test the recipe out by sending an email to yourself (From within the system so that your username matches and satisfies the recipe condition). Then check your folders to see is there is now one called IN.testing with the email you just sent in it.

Now you can create recipes to sort your mail however you like. You can even sort mail directly to the “trashbin.” Say, for instance, you never want to read anything Sprague has to say to you:

This recipe will delete all mail sent to you from user “sprague”:

:0:
* ^From.*sprague
/dev/null

If you are on a mailing list then you might want to sort all mail from that list into its own folder– or you could sort mail from multiple lists all into one folder. E.g., if you are on both the cyberlaw@cs.fsu.edu and cop4531@cs.fsu.edu lists, then you could sort all mail from those lists into a “classes” folder like this:

:0:
* ^(from: | cc: | to:).*(cyberlaw | cop4531)
classes

See also the section Using the Extraction Operator and $MATCH to Sort Mailing List Messages in
Procmail Quickstart

Recipe Metacharacters

 

Metacharacter Meaning
. any character except a newline
(string) treat string as a single item
* zero or more of preceding item
? Either zero or one of the preceding item
a? Either zero or one a
+ one or more of preceding item
^ beginning of line
$ end of line
[character list] any single character in character list
[^character list any single character NOT in character list
[^-a-d] any character which is not either a dash, a, b, c, d, or newline
\<string\> the word string
| or

 

Note: If you need to represent a literal character that is reserved for on of the above special purposes you can use the ‘\’ (backslash) escape character. For example, if you needed a literal “^’, then you would type \^.

Similarly, a period in an email address is represented as such:

* ^Reply-To: spammer@spam\.house

The difference between a word and a string:

If you search for the string test, then procmail searches for any occurrence of those characters in that order, including within other strings. If, on the other hand, you told it to search for the WORD test then it would only look for exactly that string by itself. For example:

The STRING test would be found in the heading

Subject: testing

but the WORD test would not. It would, however, be found in

Subject: test!

One more thing: A word can only contain letters, digits, and underscores, so any whitespace or other characters(such as the ! above) serve as delimiters.

Spamassassin

See this .procmailrc file for an example of how to filter your mail through the program spamassassin. Any email spamassassin marked as spam will be sent to a folder called “spam” as it is currently configured. This way you can redirect “spam” to a folder and you can make sure it is spam before you throw it away. See Spamassassin.org for more information on spamassassin. To create whitelists in spamassassin you will need to edit a file called ‘user_prefs’ in the .spamassassin folder that is created in the root level of your homedirectory the first time you run spamassassin. In case you find that spamassassin filters email from addresses you’d rather it didn’t, you may add white/black list entries,, as many as you like, in your user_prefs file(see below). Remember that procmail filters recipes in the order they appear in your .procmailrc file, as well though, because if you have any special filters that will catch one of these addresses anyway, then you can just put that recipe before the one that sends mail to spamassassin (which would, incidentally, be much faster).

# Whitelist and blacklist addresses are now file-glob-style
#patterns, so “friend@somewhere.com”, “*@isp.com”, or
#”*.domain.net” will all work.
# whitelist_from someone@somewhere.com
whitelist_from *uiuc.edu
whitelist_from *@friends.com
whitelist_from meghan@colleagues.com

blacklist_from *@spamhut.com
blacklist_from *@massamrketing.com
blacklist_from *@microsoft.com

When/if you are confident that spamassassin does the job the way you want it, this .procmailrc is the same as the one above, but it sends the messages to /dev/null instead.

Even more recipes can be found in this sample .procmailrc file.Make sure to read through this file as it will need to be modified to work in your particular environment.

Mailstat

You can use mailstat to track your incoming messages, including any errors that occurred in processing your mail, how many emails you have received since the last time you ran mailstat, and where procmail has sent them. You must run mailstat from Diablo.

To get help on mailstat, type:


mailstat -h

To run mailstat, type:



mailstat $HOME/Procmail/log

Where $HOME/Procmail/log is the value of your LOGFILE variable in .procmailrc. Running the command moves your log file to log.old and displays a tabular description of your log similar the following:

  Total  Number Folder
  -----  ------ ------
   9964    4    IN.testing
   3408    1    classes
  -----  ------
  13372    5

Unless you delete your logfile from time to time it will keep growing larger and larger