in reply to Email::MIME Distinguishing HTML msg from HTML attachment

If message contains the same message in text/plain and text/html, then its type is multipart/alternative. If it contains text/plain message and text/html attachment, then its type multipart/mixed.

  • Comment on Re: Email::MIME Distinguishing HTML msg from HTML attachment

Replies are listed 'Best First'.
Re^2: Email::MIME Distinguishing HTML msg from HTML attachment
by cormanaz (Deacon) on Jul 23, 2009 at 15:31 UTC
    Thanks zwon. Here is code to get html alternative if it is available, text otherwise, in case anyone can use it.

    Steve

    use Mail::POP3Client; use Email::MIME; my $pop = new Mail::POP3Client( USER => 'user@mail.com, PASSWORD => 'foobar', AUTH_MODE => 'PASS', HOST => "mail.com" ); for( my $i = 1; $i <= $pop->Count(); $i++ ) { my $message = $pop->HeadAndBody($i); my $parsed = Email::MIME->new($message); my ($body,$type) = getbody($parsed); # do something with body } sub getbody { my $parsedmessage = shift; my $body; my $bodytype; my @parts = $parsedmessage->parts; if ($messagetype =~ /^multipart\/alternative/) { # it's a plain alternative so get the html body in the second +part $body = $parts[1]->body; $bodytype = 'html'; } elsif ($messagetype =~ /^multipart\/[mixed|related]/) { # it's an alternative or related so check the first part my $subtype = $parts[0]->content_type; if ($subtype =~ /^multipart\/alternative/) { # it's an alternative with attachments so get the body fro +m the # second subpart of the first part my @subparts = $parts[0]->parts; $body = $subparts[1]->body; $bodytype = 'html'; } else { # otherwise it's plain text with an attachment so get the +body # from the first part $body = $parts[0]->body; $bodytype = 'text'; } } else { # it's plain text with no attachments so get the body # from the first part $body = $parts[0]->body; $bodytype = 'text'; } return ($body,$bodytype); }