You could use Email::MIME, but I think you'll somehow have to handle the separate cases...  As multipart messages can in theory be arbitrarily nested, a recursive approach is appropriate.  The following example returns the first acceptable (i.e. plain text or html) part found:

use Email::MIME; sub handle_parts { my $part = shift; my $content_type = $part->content_type; #print "Content-Type: $content_type\n"; # debug my $body = $part->body; if ($content_type =~ m#text/plain#) { return $body; } elsif ($content_type =~ m#text/html#) { return html2text($body); } elsif ($content_type =~ m#multipart/#) { for my $subpart ($part->parts) { my $text = handle_parts($subpart); return $text if defined $text; } } return; } sub html2text { my $html = shift; # my $text = ... (left as an exercise) # return $text; return $html; } my $message = ... my $parsed = Email::MIME->new($message); my $text = handle_parts($parsed);

You might need some additional checks to figure out if the first content part is the one you want...


In reply to Re: Reading POP email that may be plain text, HTML, or both by almut
in thread Reading POP email that may be plain text, HTML, or both by pdxperl

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.