The FreeBSD Diary

The FreeBSD Diary (TM)

Providing practical examples since 1998

If you buy from Amazon USA, please support us by using this link.
procmail - a simple example 8 December 2000
Need more help on this topic? Click here
This article has no comments
Show me similar articles
procmail is a tool for manipulating mail.  If you like to sort your incoming mail into various folders/directories, procmail can do that for you.  If you want to cut down on the spam, procmail can help with that.   Basically, if you can write a rule for it, procmail can do it.

I wanted to sort incoming mail for a list archive box.  Mail for one list had to go to one mailbox and from another list for another mailbox.  This seemed pretty easy to do with procmail.

The home page for procmail is

Here's a link to procmail in FreshPorts.

And I used some of this FAQ.   But Timo's procmail tips and recipes was very useful once I started creating my procmail rules.

The first step was to install procmail from the ports.  I followed the instructions found in the FreeBSD handbook for compiling ports from the internet.
cd /usr/ports/mail/procmail
make install

Don't do a make clean until after you grab the examples during the configuration later in this article

Introducing .forward
I'll give you a very simple example.  procmail is often invoked from your .forward file.  .forward resides within your home directory.  It is used by sendmail, and other mail transport agents (MTAs).  .forward, if it exists, is used each time a new message is delivered to that user.  The conventional use of .forward is for redirecting mail.  See man forward for more information.

procmail makes use of .forward to process your mail according to the rules you specify within .procmailrc.

Here is what I put in ~/.forward:

[dan@set:/home/dan] # more .forward 
"|exec /usr/local/bin/procmail || exit 75"

The above is taken from man procmail, in the NOTES section.  Be sure to include the quotes.

procmail rules
Here is the example rule set I'm using.  This example was taken from

Your procmail version may vary so the above directory may differ on your box.

[root@set:/home/dan] # more .procmailrc
# Please check if all the paths in PATH are reachable, 
# remove the ones that are not.

MAILDIR=$HOME/mail      # You'd better make sure it exists

:0                    # Anything from Henry
* ^From.*henry
henries               # will go to $MAILDIR/henries
:0                    # anything from cvs-all mailing list
* ^Sender.*owner-cvs-all@FreeBSD.ORG
lists/cvs-all         # will go into my archive

# Anything that has not been delivered by now will go to $DEFAULT

Be sure to create the mail directory indicated above:

mkdir ~/mail

That should be about it.  Try it and see.

Sometimes it can be difficult to test your procmail rules.  You can't always wait for a mail message which meets your test conditions.  Fortunately, there is another way.  Take a message, including the headers, and copy it to a file.   Then pipe the contents of that file to procmail.  I found the testbench as described at Timo's procmail tips and recipes very helpful.
Clean up
Remember to do a make clean!
Recipes 31 December 2000
Gill wrote in with this recipe for mailing lists.  It was actually written by Neil Blakey-Milner.

I am sure you will get hounded with people suggesting thier own procmailscripts, but this one came to me by way of the -questions list and it is the niftiest one i've ever seen and it works perfectly. It is useful for
all of the FreeBSD lists:

# From: Neil Blakey-Milner
# Cc:
# Subject: Re: Procmail recipes for fbsd lists

# I use:

* ^Sender:.owner-freebsd-\/[^@]+@FreeBSD.ORG
* LISTNAME??^\/[^@]+

# It can probably be simplified, but it expands as necessary when I
# join new lists, &c.

Chad Ziccardi wrote in with these two URLS:

Need more help on this topic? Click here
This article has no comments
Show me similar articles