[ILUG] script process mboxes add headers

Pete McEvoy pete at yerma.org
Mon Aug 6 15:55:09 IST 2007


On Thu, Aug 02, 2007 at 03:55:07PM +0100, Lars Hecking wrote:
> > Does anyone know how to process all messages in a mbox, taking the data 
> > from the message-id header and adding it to a new X header?
> 
>  I think it's a bit more complicated than what you outlined: you need to run
> 
>  formail -s procmail -m rcfile <inbox >output
> 
>  to split the inbox into single emails and let procmail process each
>  individually with the recipe(s) in rcfile. The procmail recipe will
>  extract the message-id: header, and then use formail to append or
>  insert the new XUIDL header. Something along the lines of
>  (completely untested)
> 
>  :0 fwh
>  * ^Message-ID: \/[^ ]*
>  |formail -I "X-UIDL: $MATCH"
> 
>  give or take a few flags or options.

Hi Lars.
Thanks for replying. 

I played around with your procmail recipe, which seemed to be exactly
what I need, but due to my complete lack of knowledge of procmail, I was
never able to get it to produce anything other than an empty file in
output. 

In the end, a friend helped me knock up the following using
libmail-box-perl:


#!/usr/bin/perl -w
use strict;
use warnings;
use Mail::Box::Manager;
my $dir = shift or die "Please supply me with a dir path";
 my $mbx = Mail::Box::Manager->new->open(folder => $dir,access => 'rw');
 for my $message ($mbx->messages) {
 my $head = $message->head;
 $message->write;
 $head->add('X-UIDL' => $message->messageID);
 $message->write;
}


Or, if you want to run it on a whole directory of mboxes, rather than
just one:

#!/usr/bin/perl -w
use warnings;
use Mail::Box::Manager;
my $dir = shift or die "Please supply me with a dir path";
opendir DIR, $dir or die "Can't open $dir ($!)";
my @mboxen = map "$dir/$_", grep $_ !~ m/^\./, readdir DIR;
closedir DIR;
for my $mbox (@mboxen) {
  my $mbx = Mail::Box::Manager->new->open(folder => $mbox,
                     access => 'rw');
  for my $message ($mbx->messages) {
    my $head = $message->head;
    next if $head->get('X-UIDL');
    $head->add('X-UIDL' => $message->messageID);
    $message->write;
  }
}


Cheers

-- 
Pete





More information about the ILUG mailing list