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