karun.jiju has asked for the wisdom of the Perl Monks concerning the following question:

I am using the module Mail::POP3Client in my code to read the mails and using the module attachment::stripper to get the attachments. I am facing issue in reading the body content(ie., the message part alone no attachments).Kindly suggest me a module for fetching the message part alone from the body of the mail(by body i mean the content with attachment).
  • Comment on Stripping the text part alone from mail

Replies are listed 'Best First'.
Re: Stripping the text part alone from mail
by GrandFather (Saint) on Nov 26, 2009 at 20:09 UTC

    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
Re: Stripping the text part alone from mail
by philipbailey (Curate) on Nov 26, 2009 at 14:04 UTC
    There are a number of suitable modules, but have a look, for example, at Email::MIME.