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.
My killfile recipe looks like this:
:0
* ? /bin/fgrep -i -f $HOME/.killfile
/dev/null
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.
.Trash for mbox, .Trash/ for maildir
Ah yeah, well spotted Aidan and well caught Niall.
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.
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.