Re: getting info from a nested message
Mark Overmeer <[email protected]>
| Newsgroups | gmane.comp.lang.perl.modules.mail-box |
|---|---|
| Organization | MARKOV Solutions |
| Message-ID | <[email protected]> |
* Mike J. Vincelette ([email protected]) [040109 22:59]: > Sorry for asking a "how-to", but I've looked through the modules, > documentation, and the mailing list and can't quite figure this out. Don't worry, the mailinglist was made to ask questions. > I have nested messages in the form "message/rfc822". I want to grab the > nested message AS A MESSAGE so I can query the sender, extract the > subject, and delete artificial headers before processing the message > (into a word parser). How do I do this? First: any message part is a Mail::Message::Part object which extends Mail::Message... so anything you can do with a normal message you can do with a part, although sometimes the effect is a little different, dor instance in case of 'delete()'. The best way to figura-out what you can do with an object is by accessing the HTML version of the manual pages at http://perl.overmeer.net/mailbox/html/ > This is what I have: > > my $part = $message->body->part(1); #the first part is always the > nested message > if($part->isNested) # just a test to make sure Mail::Box interprets > this correctly > { > #this is a nested part... > my $nestedmessage = ???????????? > } > > Now, how do I turn that $part into a message so I can work with it? You have to be careful, because many actions on a body will produce a new message body, and therefore be disconnected from the actual message. Changes in the header, however, are on the original. In your case, $part isa Mail::Message::Body, and $part->body is a 'Mail::Message::Body::Nested'. That $part object contains the encapsulation information (which is a header) and as body the encapsulated message. You get that one using $part->body->nested. It looks a bit complicated to write $msg->body->part(1)->body->nested but that is because you are moving on dangerous grounds. In a normal case, you should test the input you get, and never trust that the message looks as you expect. So, I would write something like: my ($part) = grep { $_->isNested } $msg->parts; die unless defined $part; my $nest = $part->body->nested; # or ($part->parts)[0] -- MarkOv ------------------------------------------------------------------------ drs Mark A.C.J. Overmeer MARKOV Solutions [email protected] [email protected] http://Mark.Overmeer.net http://solutions.overmeer.net