Skip to content. Skip to navigation

Jan Torbens homepage

You are here: Home public stuff Autolistfolder - an automatic serverside (IMAP-) folder creater with perl and procmail
Document Actions

Autolistfolder - an automatic serverside (IMAP-) folder creater with perl and procmail

by jtheuer last modified 2007-10-25 20:40

Tired of creating rules for Mailiglist, folder and so on? Do you often join and leave mailiglists? Let procmail do the work for you!

Procmail, the misunderstood tool

When I first came in contact with procmail, I refused to use it. It is not really intuitive and without a deeper knowlege of regular expressions you are not able to understand anything. Times have changed - let's rock!

A long time ago I removed my kmail rules and created server-side-rules with procmail for a simple reason: Webmail! Squirrelmail rocks when you're not at home, but a 2GB INBOX is not the way I wanted to do it. So Folder were needed. Over the years, lists came, lists went, and even with this cool KDE fish KIO slave (Link ssh ressources on you desktop) a one-click-open, one-click-save with your favourite editor was too much work for me *g*.

Perl - extract the List from the mailheaders

Lists have (should have?) a header like this:

List-Post: <mailto:mailiglistname@domain.tld>

The follwing code creates form mailinglistname@domain.tld a folder name domain,tld / mailiglistname. Don't be surprised, a point is the folder-seperator for IMAP Folder, so I had to replace '.' with ','. The foldername is printed out on STDOUT and can be read by procmail.

Withing procmail I run the script:

#Pipe the mai through the listextractor
#return value LIST: "domainpart.localpart"
:0c
* ^list-post
LIST=|perl /usr/local/bin/listextract.pl

#Save the mail inside the maillists directory
:0
$DEFAULT/.maillists.$LIST/

Here is the perl script:

#!/bin/perl

# extracts the listid from a mail via stdin and returns a valid foldername

# read by line...
while () {

  # has a list-post header?
  if ($_ =~ m/^list-post: /i) {

    if($1 ne "" && $2 ne "") {
      $localpart = $1;
      $domainpart = $2;

      # . replace with ,
      $domainpart =~ s/\./,/g;

      # . replace with _
      $localpart =~ s/\./_/g;

      print $domainpart . "." . $localpart;
      exit 0;
      }
    else {
      die "unexpected error: list-post found, but not parsable. ";
      }
    }
  }
die "no list-post header found - this is NOT a list mail\n";

It's amazing how easy it was to create this little scripts (full files are attached). Please feel free to contribute comments!

Related content

Just scan the header

Posted by cornelis@pcornelissen.de at 2008-05-07 17:45
Hi!

I've just seen your script. I'd suggest to tweak the "while()" line.
The header ends at the first empty line of the message, so the script should stop there too, otherwise it may parse forwarded mailinglist mails as mailinglistmeil too, which doesn't seem to be the expected behavior.

Just scan the header

Posted by jtheuer at 2008-05-07 18:10
Thanks for your comment! I'll change it!