Interesting. Many if not most HTML messages are sent multipart--several MIME types in one message, including at minimum an HTML "part" for HTMLized email clients and a plain text part for older email clients or people who prefer to read their messages in that format. Any attachments will also have their own MIME part of the appropriate type.

I used Email::MIME, which no one has yet mentioned, to take emails I send from my mobile phone and turn them into Web posts, with plain text and/or JPEG photo. Here is a modified untested version of that code which may suit you purposes:

my $parsed = Email::MIME->new($message) or die "Could not parse email +message: $!"; #$message is full text of entire email m\ essage foreach my $part ($parsed->parts) { if ($part->content_type =~ /text\/plain/i) { #You have a plain text part #Do stuff here with $part->body } elsif ($part->content_type =~ /image\/jpeg/i) { #You have a JPEG part #in $part->body } elsif ($part->content_type =~ /text\/html/i) { #You have an HTML part #in part body my $html = $part->body; my $plain_text; my $parsed_text = HTML::TokeParser->new(\$html) or die "Cannot rea +d message text for parsing and cleaning: $!"; while (my $token = $parsed_text->get_token) { if ($token->[0] eq 'T') { # text $plain_text .= $token->[1]; } } #Do stuff with $plain_text extracted from HTML here } }

Notice the HTML::TokeParser part inside the HTML section. You'll only want to use that if the plain text part is unavailable to you. HTH.


In reply to Re: Extracting TEXT from email by Anonymous Monk
in thread Extracting TEXT from email by ady

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.