[prog] Parsing mail headers in Perl

Wolfgang Petzold petzold at villa-chaos.de
Mon Jun 30 14:14:48 EST 2003


Hello!

Dan Richter, 30.06.03:

> Parsing single-line mail headers is easy, but multi-line messages present a
> challenge. Here's what I came up with:
>
> if ( $headers =~ /Received: (.*?)\n[^\t ]/ ) { ... }
>
> This works when the Received line has no newlines, but not when it has
> several lines. (In the latter case, there's no match at all.)

Have you tried to use the "s -- Treat string as single line" option to the
match operator?

If I pipe your email through

-------------------------------------------
#!/usr/local/bin/perl -l
use strict;
use warnings;

# Gather all the header lines together in one string, $headers.
#
my $headers = "";
while (<>) {
        last if (/^\s*$/);
        $headers .= $_;
}

while ($headers =~ /Received: (.*?)\n\s+(.*?)\n/gcs) {
	# watch for the options:             -->^^^<--
	#
        print "MATCH:";
        print "    first line: <$1>";
        print "    second line: <$2>";
}
-------------------------------------------

I will get MATCHes for every "Recieved:" line in the mail header. See
"perldoc perlop" for the "cgimosx" options to the match operator.

Wolfgang



More information about the Programming mailing list