In the previous recent thread parsing and printing e-mail, keszler suggested the Email::MIME module.

Here's a test program that uses Email::MIME to pull apart a full message. You should be able to figure out how to isolate just the parts you want.

#!/usr/bin/perl -w use strict; use warnings; use Email::MIME; my $body = join("",(<DATA>)); my $email = Email::MIME->new($body); print "===== Top Header =====\n"; print "From: ".($email->header("From"))."\n"; print "Date: ".($email->header("Date"))."\n"; print "Subject: ".($email->header("Subject"))."\n"; print "\n===== Top Body =====\n"; print "".($email->body())."\n"; my @parts = $email->parts(); print "There are ".@parts." parts in this message.\n"; my $p = 0; foreach my $part (@parts) { $p++; print "\n===== Part $p Header =====\n"; print "Content-Type: ".($part->header("Content-Type"))."\n"; print "Content-Transfer-Encoding: ".($part->header("Content-Transf +er-Encoding"))."\n"; print "\n===== Part $p Body =====\n"; print "".($part->body())."\n"; } __DATA__ From: homer@simpson.net To: marge@simpson.net Subject: Bart and Lisa Date: Thu, 17 Sep 2009 12:17:23 -0700 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0009_01CA7F30.7F3A37C0" Mime-Version: 1.0 This is a multi-part message in MIME format. ------=_NextPart_000_0009_01CA7F30.7F3A37C0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit From: sender@email.address Sent: Wednesday, December 16, 2009 9:28 PM To: test@email.address Subject: This is the subject This is the body. ------=_NextPart_000_0009_01CA7F30.7F3A37C0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable <html> <head> <body> This is the body. </body> </html> ------=_NextPart_000_0009_01CA7F30.7F3A37C0--

Output from the program:

===== Top Header ===== From: homer@simpson.net Date: Thu, 17 Sep 2009 12:17:23 -0700 Subject: Bart and Lisa ===== Top Body ===== This is a multi-part message in MIME format. There are 2 parts in this message. ===== Part 1 Header ===== Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit ===== Part 1 Body ===== From: sender@email.address Sent: Wednesday, December 16, 2009 9:28 PM To: test@email.address Subject: This is the subject This is the body. ===== Part 2 Header ===== Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable ===== Part 2 Body ===== <html> <head> <body> This is the body. </body> </html>

In reply to Re: best module to read e-mail from a POP3 account by gmargo
in thread best module to read e-mail from a POP3 account by keiusui

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.