Automatic folders for mailing lists using procmail

Here is a quick tip which I have gleaned from multiple sources which makes using procmail filters a breeze. I subscribe to dozens of mailing lists, and it really is somewhat of a chore to create filters for every mailing list I signup for so that mails for that list goes to a separate folder.

However, it is possible to setup procmailrc in such a way that you really don’t need to update procmailrc for every new mailing list that you signup for. You essentially use procmail’s regex support to generate folder names.

At the end of procmailrc, you just need to add this rule:

:0  
* ^((List-Id|X-(Mailing-)?List): *<\/[^.]*)  
.list-$MATCH/  

This will use the list-id header value, extract the text before the first period (e.g. for a list-id xxxx.lists.sourceforge.net, it will extract xxxx) and generate a folder name called .list-xxxx to store the mail in. If the folder doesn’t exist, it will create it.

Now I mention to add this at the end of the procmailrc so that it works kind of like a catch-all recipe. You can have other recipes before this which work differently on incoming mail, which stores mails for lists into folders which don’t quite match the list name, etc. Whichever mailing list mail gets past these custom filters, they would hit the recipe given above and go into the “magic” folder for the list.

However, if you really want to keep this recipe at the top of procmailrc and want to restrict this “automatic folder creation” treatment to a select few lists, you can use a variation of the recipe which still saves you some effort.

:0  
* ^((List-Id|X-(Mailing-)?List): *<\/[^.]*)  
{  
    LISTID=$MATCH  

    :0  
    * LISTID ?? (ilugd|ilugd-announce|bangpypers|linux-india)
    .list-$MATCH/  
}

Now whenever you subscribe to a new list, edit the line with “??” above, and add a “|listname” before the final brace and you are set. This recipe can stay at the top of the procmailrc, automatically filtering only the lists you mention here. The mails from mailing-lists which don’t match these lists will keep looking further down for the rest of the procmail recipes.

I must mention at the end, that if you are using IMAP to access this mail store where procmail is doing something similar to a server-side filtering, then after you incorporate this procmailrc recipe, wait for the first mail to arrive and then use the “subscribe” option of your IMAP client to subscribe to the new folder. Most IMAP clients don’t automatically subscribe to new server-side created folders. So even if procmail would be creating the new folder, it won’t be visible in your IMAP client unless you subscribe to it, and you might get the impression that the recipe is not working. That is unless you are using procmail’s log feature to debug new recipes already. :)

 
comments powered by Disqus