http://www.perlmonks.org?node_id=186179

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

I am using MIME::Parser to parse email messages. The problem is that I can not get the electronic address (joe@somewhere.com) from the header, only "Joe". MIME::Parser does not seem to read the <joe@somewhere.com> part at all, how do I retrieve it? The file, test.eml contains the following email:
Message-ID: <001401c237bd$138f72f0$0100007f@WM> From: "Joe" <joe@somewhere.com> To: "Mary" <mary@test.com> Subject: Test Date: Tue, 30 Jul 2002 13:34:33 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 test
The following code produces the dump at the bottom:
my $oParser = new MIME::Parser; $oMail = $oParser->parse_open("test.eml"); print $oMail->print();
Message-ID: <001401c237bd$138f72f0$0100007f@WM> From: "Joe" To: "Mary" Subject: Test Date: Tue, 30 Jul 2002 13:34:33 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 test
Thank you for your help!

Replies are listed 'Best First'.
Re: MIME::Parser does not retrieve
by RMGir (Prior) on Jul 30, 2002 at 12:40 UTC
    What's your version of MIME::Tools? I don't see that problem here.
    h:\>type test.pl use MIME::Parser; my $oParser = new MIME::Parser; $oMail = $oParser->parse_open("test.eml"); print $oMail->print(); print "\n\nPerl version: ",$],", M::P version:",$MIME::Parser::VERSION +,"\n"; h:\>perl test.pl Message-ID: <001401c237bd$138f72f0$0100007f@WM> From: "Joe" <joe@somewhere.com> To: "Mary" <mary@test.com> Subject: Test Date: Tue, 30 Jul 2002 13:34:33 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 test 1 Perl version: 5.006001, M::P version:5.406
    test.eml contains your test data.
    --
    Mike
      Thanks for your reply Mike. I have been running the script in a browser, and the <> tags translated "Joe" <joe@somewhere.com> to Joe, hiding the address!!! When I saw your dump from the command prompt it dawned on me. So we live and learn...
Re: MIME::Parser does not retrieve
by hacker (Priest) on Jul 30, 2002 at 13:33 UTC
    How about using Mail::Internet instead? Here's how I'm using it, which works well:
    use strict; use warnings; use Mail::Internet; # parse incoming headers # from a stream, could be # a file handle too.. my $message = new Mail::Internet ([<>]); my $from = $message->get('From'); my $subject = $message->get('Subject'); my @body = @{$message->body()}; # ..print or manipulate as normal.
    In my code, I'm using Mail::Internet , MIME::Lite (thanks ar0n for the tip), and Config::Simple to parse a template out of an email message sent to my script, operate on the variables parsed from that template, and send the sender back a binary file which includes the data requested in the template, as a file attachment to the reply.

    These modules let me grab all the headers, body, and manipulate them/parse them for my needs. It works very well, and is incredibly small and fast.