I have struggled a lot getting my procmailrc to work properly, so I thought I would compile what I have learned now that it actually works. A small disclaimer though: A more thorough reference is here, and almost all the information I have here came from that page, but I have also used some tutorials scattered across the internet if it was something I didn’t really understand.

First we need to set a couple of variables which tells procmail how to behave and which folders it should use. In the procmailrc the variables are stored as NAME=VALUE and retrieved by $NAME, much like it is in bash. The variables we need are DEFAULT, MAILDIR and PMDIR. It is smart to also set LOGFILE and perhaps VERBOSE. In my procmailrc I have also set a prefix path which I then use on all the other folders. so my configuration becomes something like this:

CORRECTHOME=/home/username

MAILDIR=$CORRECTHOME/Maildir/
DEFAULT=$CORRECTHOME/Maildir/
PMDIR=$CORRECTHOME/
LOGFILE=$PMDIR/.mail.log
VERBOSE=off
  • CORRECTHOME: This is my home folder
  • MAILDIR, DEFAULT: This is where procmail should put my mail. Notice the slash after the folder name. It is needed for it to use the Maildir format. If you do not have it, it will store mail as in the mbox format.
  • PMDIR: This is where procmail should look for configuration files and store log files
  • LOGFILE: This is the path to the logfile
  • VERBOSE: Should procmail output verbose information to the logfile or not. I have never needed as much information as I get from the verbose output, so I have just turned it off.

After this we get to the recipes (or rules as I like to call them, but that is not completely accurate):

  • A colon line which tells procmail what it should look for when filtering. The simplest colon line is just :0: (thats a zero, not the letter O). I haven’t used any other colon lines than that one, but it is possible to read more here.
  • A condition. This can be empty or a rule. The I have only used regular expression rules, but it is possible to test on exit code of external programs, size of the message or to give messages scores. The regular expression I use the most is the parenthesis: * ^FROM: *([email protected]|[email protected]) this will match all mail from either [email protected] or [email protected]. Note that regular expressions are by default case insensitive.
  • Action line. This is what procmail should do to the mail. Note that you can only have a single action line for each colon line. If you need multiple actions for one colon line, you can use curly braces, but that is not something I have experimented doing. An action can be either to choose where to save the mail, forward it, or pipe it through a shell command.

Here is an example of an recipe

:0:
* ^(From|Cc|To).*@invoicia.no
.INBOX.Invoices/

This will take all mails which is related to the domain invoicia.no (either from, or where they are put on CC or recipient) and put it in the Maildir .INBOX.Invoices/ which is where I keep all my invoices.

Of course you can have multiple colon lines after each other, and I will recomend you to end your procmailrc with a line which will save all the mail to the default mailbox like this:

:0:
$DEFAULT

I hope this was helpful, and at last you will see my procmailrc (with some changes to make it a bit more anonymous and to make sure that email scrapers won’t start spamming everyone I know). Note the pseudo variable TO_ which I use in the second to last recipe. This will move all mails where the recipient is spam on my domain to the Spam folder in my inbox.

CORRECTHOME=/home/<myusername>
MAILDIR=$CORRECTHOME/Maildir/
DEFAULT=$CORRECTHOME/Maildir/
PMDIR=$CORRECTHOME
LOGFILE=$PMDIR/.mail.log
VERBOSE=off

:0:
* ^From: *([email protected]|[email protected]) *$
.INBOX.MyInvoices

:0:
* ^TO_\<spam address on hjortland.org\>
.INBOX.Spam/

:0:
$DEFAULT