Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Email::MIME Distinguishing HTML msg from HTML attachment

by cormanaz (Deacon)
on Jul 15, 2009 at 17:48 UTC ( [id://780412]=perlquestion: print w/replies, xml ) Need Help??

cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Howdy Bros. I am using Email::MIME to process some e-mails and have run up against a problem. Some email clients send a message in two parts, both in text/plain and text/html. This is of course easy to sort out.

Someone using the same sort of client might also include an html attachment. Again easy to sort out by just taking the first html part you find.

BUT, what if someone sends a plain text (only) message AND includes an html attachment? How do you tell the html part is an attachment and not just an html version of the message? I've been looking at the headers returned by Email::MIME and I'm not sure it's possible to distinguish. Or am I missing something?

TIA...

Steve

Replies are listed 'Best First'.
Re: Email::MIME Distinguishing HTML msg from HTML attachment
by zwon (Abbot) on Jul 15, 2009 at 18:25 UTC

    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.

      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); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://780412]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found