Beloved Brethren and Sistren:

I am stymied over what I would think is a simple matter. I am working on an e-mail program, using the Net::POP3 module. This module returns a pointer to an array of text that contains the entire message, both headers and body of the message. I am trying to figure out a way to get rid of the header information and preserve the body of the text.

I checked the documentation of Net::POP3, and it doesn't appear to have a way that I can simply ask for just the body text. I checked RFC 1939 and RFC 2822, and found that headers have the form <HeaderID> : <Header Text>. This is a big help, but not enough. Headers can span more than one line. When they do this, the subsequent lines begin with a whitespace.

I was trying to reconstruct the headers that were split into more than one line by finding newline/whitespace combinations, and replacing them with a space. This turned out to be quite difficult for me. First I tried:

$line =~ s/\n\s+/ /mg;

That doesn't work. I think the problem is that each line ends with a newline. There are no lines that contain a newline followed by another character.

I then reasoned that, since the newline is followed by whitespace, I might be able to search for lines that begin with whitespace (and simply remove those lines) using

$line =~ s/^\s+.*/ /g;

Unfortunately, this finds any such lines, including those contained in the body text. I want to preserve all of the text, and simply remove (or at least identify) the header lines.

So the question is, how can I reconstruct the headers that span more than one line? Or, equally useful, how can I distinguish headers from body text?

UPDATE: Thanks to all of you who have commented. You've given me some great ideas to try. I appreciate it.


In reply to 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.