parsing email

Dirk Koopman djk at tobit.co.uk
Thu Nov 23 23:00:06 GMT 2006


On Thu, 2006-11-23 at 19:00 +0000, Sam Smith wrote:
> On Thu, 23 Nov 2006, Simon Cozens wrote:
> > Sam Smith:
> >> Given people here wrote half the Mail:: modules, is there
> >> a nice overview module which exists which parses common
> >> random varieties of mail.
> >
> > What's the big picture? What are you trying to do? If it's
> > what it sounds like, you might want Email::Store. If not,
> > it's bits and pieces time, I'm
> 
> Dump various bits of information (subject,from,body) from
> emails into a database for random display. The email itself
> wont be stored.

Mail::Internet has what you require. 

Things like:-

my $msg = Mail::Internet->new(\*STDIN) or die "Mail::Internet $!";
my $head = $msg->head->header_hashref; 

if ($head) {
        if ($head->{Subject}->[0] =~ /wwv/i) {
                process_wwv($msg);
        } elsif ($head->{From}->[0] =~ /rwc\.boulder/i || $head->{'From
'}->[0] =~ /rwc\.boulder/i) {
                process_solar($msg);
        }
}

and in one of the process_* routines:-

sub process_wwv
{
        my $msg = shift;
        my @out;
        my $state;

        foreach (@{$msg->body}) {
                next if /^\s*:/;
                next if /^\s#/;
                next if /^\s*\r?\n$/s;
	
		...
	}
}

should do it for you.

Dirk



More information about the london.pm mailing list