in reply to Reading POP email that may be plain text, HTML, or both

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...

Replies are listed 'Best First'.
Re^2: Reading POP email that may be plain text, HTML, or both
by pdxperl (Sexton) on Jun 20, 2010 at 03:08 UTC
    Thanks! This approach worked quite well. It seems like it would be a useful function to add to one of the many email modules on CPAN