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

I have a MIME multipart message and one of them is a mail in itself i.e Content-type: message/rfc822. I need a to parse the original message and get back the parts as a whole mail. I tried using MIME::Parser, but in that case I lose the headers of the attached mail and get just the body as a txt file.

Replies are listed 'Best First'.
Re: Parsing a multipart mail
by gellyfish (Monsignor) on Mar 01, 2005 at 13:06 UTC

    I would suggest using Email::MIME - you can do something like:

    use strict; use warnings; + use Email::MIME; use Email::Folder::Mbox; + my @received; my $box = Email::Folder::Mbox->new('/home/jonathan/mail/spam'); while ( my $mail = $box->next_message ) { my $main = Email::Simple->new($mail); + if ($main->header('Content-Type') and $main->header('Content-Type') + =~ /multipart/i ) { my $mess = Email::MIME->new($mail); + foreach my $part ($mess->parts()) { if ($part->content_type() =~ m%message/rfc822%i ) { print $part->as_string(); } } } + }

    /J\

      With MIME::Parser, you can get the full rfc/822 message by using the $part->body_as_string method and feeding that back into a new/reused MIME::Parser object.
Re: Parsing a multipart mail
by Limbic~Region (Chancellor) on Mar 01, 2005 at 13:51 UTC
Re: Parsing a multipart mail
by InfiniteLoop (Hermit) on Mar 02, 2005 at 05:22 UTC