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

Hi I'm looking for a way to access embedded email. That is, I have an email with another email as attachment, I need to open that embedded email to find out emailaddess in its content by regular expression. I found some code which can get attached file and save it to somewhere. But that's not what I need because my attachment is an email, not text/word or excel file. It'll be highly appreciated if somebody who has this kind of knowledge/experience can tell me which module I should use and where to find some example code.

Replies are listed 'Best First'.
Re: Perl to open embedded email?
by halfcountplus (Hermit) on Oct 07, 2009 at 20:28 UTC
    If you use an mbox style mailbox (most people do) you can parse it in all kinds of crazy ways (eg, pull all images, etc) with Mail::Box::Manager. Actually that may work with any kind of mailbox.

    Here's a simple program to start you off:
    #!/usr/bin/perl -w use strict; use Mail::Box::Manager; my $mgr = new Mail::Box::Manager; my $box = $mgr->open(folder=>"/you/mail/somebox"); my $c = 0; while (my $msg = $box->message($c)) { my @sender = $msg->from; my $id = $msg->messageId; print "\n$id\n$sender[0][0]\n"; $c++; }
    That will print the message id number and sender for each message. To deal with attachments, you want to look into $msg->parts. Note that a "folder" here is actually a single file.
Re: Perl to open embedded email?
by zwon (Abbot) on Oct 07, 2009 at 19:53 UTC
    I found some code which can get attached file and save it to somewhere. But that's not what I need

    You can get attached email in the same way as you getting attached file, and then parse it as usual email message.

      zwon: Thanks for your reply. But how to parse that embedded email. For example, in following code usiong Win32::OLE, it seems I can get that email first (1st line), but then it doesn't work when I try to get the email body. my $attach = $folder->Items->Item($i)->Attachments(); # this line works my $attachemailbody= $folder->Items->Item($i)->Attachments()->Body; # this line doesn't

        I didn't expect that you're using OLE. I'm a GNU/Linux user, so can't help you much with OLE, but perhaps you can get the message in some more digestible form and use something like Email::MIME to parse it. Can you show the output of:

        my $attach = $folder->Items->Item($i)->Attachments(); use Data::Dumper; warn Dumper $attach;