Procmail recipes

I’ve been using procmail for many years now to filter mails into IMAP folders and a few other neat things. Here are a couple of tips or tricks I want to share;

The first one is a way of killfiling/blacklisting or plonking particular email addresses, perhaps a user on a mailing list who grates on your nerves:

:0i
* ? /usr/bin/formail -x"From" -x"From:" -x"Sender:" \
-x"Reply-To:" -x"Return-Path:" -x"To:" \
| /bin/egrep -is -f $HOME/.blacklist
/dev/null

.blacklist is a file containing the email address of miscreants. Put this recipe near the top of your ~/.procmailrc so it will be one of the first to be processed.
Replace “/dev/null” with “.Trash” if you want to be more cautious and instead send mails to your IMAP trash folder.

Say you want to perform a command, such as sending an SMS message (using o2sms for example) upon receipt of a particular email and then filter the email to a folder, try something like this:

:0c
* ^Subject: .*Important Topic*.
{
:0ci
| echo "New email from`/usr/bin/formail -x"From:"` at `date '+%X %Z %x'`"\
| $SMS $MYNUM >> $HOME/.smslogfile

:0
.Somefolder/
}

$SMS and $MYNUM are defined with the rest of the constants at the top of the top of my ~/.procmailrc as the path to my o2sms script and my mobile number respectively.

Suggestions, corrections and improvements welcomed.

6 Responses to “Procmail recipes”


  1. 1 Niall

    My killfile recipe looks like this:

    :0
    * ? /bin/fgrep -i -f $HOME/.killfile
    /dev/null

  2. 2 Aidan Kehoe

    Replace “/dev/null” with “.Trash” if you want to be more cautious and instead send mails to your IMAP trash folder.

    Should be “.Trash/” , as I understand it? Happily, there’s only one email forum participant I don’t want to read after all these years, so my killfile recipe is simpler.

  3. 3 Niall

    .Trash for mbox, .Trash/ for maildir

  4. 4 Cliph

    Ah yeah, well spotted Aidan and well caught Niall.

  5. 5 Aidan Kehoe

    A friend uses this for automatic archiving of mailman lists, without the need for list-specific configuration (make sure to revert:

    :0
    * ^List-Id:.*<\/[^>]+
    { DUMMY=`echo "$MATCH" | tr "\." "-"` }
    
    :0 a:
    .${DUMMY}/

    Which is very impressive, but breaks if your mail client wants to read your .procmailrc and find out the existing lists from it.

  6. 6 Niall

    Oh, another one I thought of…
    I usually subscribe to lists as username+listname@mydomain.
    I’ve set my MTA to deliver to procmail and in my procmailrc I have the following:

    ARG=$1
    :0
    * ! ARG ?? ^$
    $MAILDIR/.Lists.$ARG/

    $1 is the local part after the +, but you can’t use it directly in procmail, you have to assign it to a variable.
    Automatically chucks mail into the right folder.

Leave a Reply