I use MIME::Parser. Be aware however that MIME documents are multipart and are commonly recursive so you generally have to have an understanding of the structure of your document to be able to extract the bits you want. The following code may get you started:

use strict; use warnings; use MIME::Parser; my $email = <<'EMAIL'; Return-path: <grandfather@some.where.com> Received: from localhost.localdomain (0.0.0.1) by some.where.com (Merc +ury/32 v4.61) with ESMTP ID MG016DE1; 24 Nov 2009 17:42:04 +1300 MIME-Version: 1.0 Content-Transfer-Encoding: binary Content-Type: multipart/alternative; boundary="_----------=_1259037666 +48720" X-Mailer: MIME::Lite 3.027 (F2.77; T1.28; A2.04; B3.07; Q3.07) Date: Tue, 24 Nov 2009 17:41:06 +1300 Subject: Sample To: grandfather@some.where.com From: some.one@some.where.com This is a multi-part message in MIME format. --_----------=_125903766648720 Content-Disposition: inline Content-Transfer-Encoding: 8bit Content-Type: text/plain Hello World. --_----------=_125903766648720-- EMAIL my %fields = ParseMail ($email); print $fields{body}; sub parseParts { my $savedText = ''; for my $part (@_) { my $type = $part->effective_type (); if (-1 < index $type, 'multipart') { my @subParts = $part->parts (); $savedText = parseParts (@subParts); } elsif ($type eq 'text/plain') { return $part->stringify_body (); } elsif ($type eq 'text/html') { my $str = $part->stringify_body (); my $tree = HTML::TreeBuilder->new_from_content ($str); $savedText = $tree->as_text (); } } return $savedText; } sub ParseMail { my ($emailStr) = @_; my $parser = new MIME::Parser; my %fields; $parser->tmp_to_core (1); $parser->output_to_core (1); my $entity = $parser->parse_data ($emailStr); my @parts = $entity->parts (); my $head = $entity->head (); $fields{subject} = $head->get ('subject') || ''; $fields{from} = $head->get ('from'); $fields{ccList} = $head->get ('Cc'); $fields{date} = $head->get ('Date'); if (! @parts) { $fields{body} = $entity->bodyhandle ()->as_string (); } else { $fields{body} = parseParts (@parts); } return %fields; }

Prints:

Hello World.

True laziness is hard work

In reply to Re: Stripping the text part alone from mail by GrandFather
in thread Stripping the text part alone from mail by karun.jiju

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.