How do I parse a mail header?

For a quick-and-dirty solution, try this solution derived from perlfunc/split:

    $/ = '';
    $header = <MSG>;
    $header =~ s/\n\s+/ /g;	 # merge continuation lines
    %head = ( UNIX_FROM_LINE, split /^([-\w]+):\s*/m, $header );
That solution doesn't do well if, for example, you're trying to maintain all the Received lines. A more complete approach is to use the Mail::Header module from CPAN (part of the MailTools package).
Back to perlfaq9