Posted by Steve on Mon 29 May 2006 at 12:45
If you're a Debian user it is likely that you're subscribed to several of the mailing lists which the project uses for discussion, development coordination, etc. There are a lot of lists available, some quiet, and some very busy. If you're subscribed to several of them you might be looking for a simple way of organizing them, thankfully procmail makes it easy.
If you're already using procmail then you will already be familiar with the idea of sorting mail into different mailboxes (mbox) or folders (Maildir). For newcomers to procmail there is a simple introduction to filtering mail with procmail available elsewhere on this site.
procmail is configured via the file .procmailrc inside your home directory (and the global file /etc/procmailrc if it exists). This file typically contains several different things:
The rules are the important parts. Using rules you can match almost all aspects of incoming mail. Typically this means that you'll ensure that all mails sent to a particular email address get filed into a single folder - or make sure that all emails sent from a particular email address get sent into another.
For example this is a rule which I use to make sure that all mails sent to the bugtraq mailing list get stored into a mailbox called bugtraq:
:0: *(Delivered-To:) mailing list bugtraq@securityfocus.com bugtraq
I have other, similar, rules for filing messages sent to particular lists, or from particular individuals. What I used to have was a number of rules for each Debian mailing list I was subscribed to which looked like this:
... ... :0: *(X-Mailing-List:).*(<debian-devel-announce@lists.debian.org>) debian-devel-announce :0: *(X-Mailing-List:).*(<debian-user@lists.debian.org>) debian-user :0: *(X-Mailing-List:).*(<debian-devel@lists.debian.org>) debian-devel ... ...
Each of these rules was very simple, and stored the mail from a particular list to a mailbox of the identical name. However these needed updating every time I subscribed to a new mailing list, and took up a lot of space making my ~/.procmailrc file look more crowded than it needed to be.
Thankfully there is a better approach. Included within the devscripts package there are some sample rules which can be used to filter Debian mailing lists in a much simpler manner.
The examples are installed to the directory /usr/share/doc/devscripts/examples/, and can be read at your leisure.
Keeping my mail in folders which I was using previously ~/Mail/debian-devel, ~/Mail/debian-user, etc, just needed the addition of this single rule:
# Sort debian mailing lists into mailboxes. # This dynamic style means when subscribing to new Debian mailing lists # no changes need be made to this file. # Even as yet none existant mailing lists are catered for. :0: * ^X-Mailing-List: <debian-.+@lists.debian.org> * ^X-Mailing-List: <debian-\/[-a-zA-Z0-9]+ debian-$MATCH
This takes advantage of procmails matching abilities to find mails which have a X-Mailing-List header matching "debian-*@list.debian.org". The second line is responsible for matching the actual name of the mailing list - and this result is stored in a variable named "$MATCH".
Using variables in procmail rules, or recipes, is explained in the manpage for procmailrc. Reading that we see the following:
\/ splits the expression in two parts. Everything matching the right part will be assigned to the MATCH environment variable.
This explains why the second line sets $MATCH up with the name of the mailing list which we use when saving to a mailbox.
(You might wonder why I manually prefix the match with "debian-", that is only done this way because I adopted the example from the devscripts file - there matchers are saved into a subfolder...)
I've been using procmail for years and have never used the \/ operator, or the $MATCH variable, but this is a perfect example of where using it can be very simple and useful.
This article can be found online at the Debian Administration website at the following bookmarkable URL:
This article is copyright 2006 Steve - please ask for permission to republish or translate.