My first thought was to suggest Email:Simple, from which you could do something like:

my $mail = Email::Simple->new($msg); my (%headers_i_care_about); foreach my $i_care_about (qw(Received Date From To Subject Return-Path)) { my @{$headers_i_care_about{$i_care_about}} = $mail->header($i_care_about); # or, if you want it as a single string, something like: # my $headers_i_care_about{$i_care_about} = # join(' ', $mail->header($i_care_about)); }

If that fails to work as you desire, then what you could fall back to is to look at the message line-by-line until you find the first completely blank line (which indicates the end of the headers, if memory serves), remembering the last line that did not begin with whitespace, and concatenating the current line with the previous one if it did, possibly on the order of:

my (%headers); my ($lastheader); foreach my $line (@msg) { last if ($line =~ m/^\s*$/); # Reached end of headers if ($line =~ m/^\s+(.+)/) { $headers{$lastheader} .= $1; } else { my @parts = split(/:/, $line, 2); $headers{$parts[0]} = $parts[1]; $lastheader = $parts[0]; } }

Hope that helps.


In reply to Re: Removing Headers from E-mail Messages. by atcroft
in thread Removing Headers from E-mail Messages. by spiritway

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.