Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

receiving email message with an attachment.

by tenny1204 (Novice)
on Feb 27, 2013 at 15:40 UTC ( #1020912=perlquestion: print w/ replies, xml ) Need Help??
tenny1204 has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. I have a question. How can I access the actual message from an email with attachments. Normally I use something like:

$entity = $parser->parse_data($msg); $parts = $entity->parts; $part = $entity->parts(0); $body = $part->bodyhandle || die "no handle"; $IO=$body->open("r"); while(defined($_= $IO->getline)){print $_;}

But this fails when there's an attachment because then there is no bodyhandle. But i am able to read and show the data of the attachment($entity->parts(1)), just not the actual email message

Maybe one can explain. Best regards, Tenny.

Comment on receiving email message with an attachment.
Download Code
Re: receiving email message with an attachment.
by Corion (Pope) on Feb 27, 2013 at 15:44 UTC

    If you don't want to die, maybe don't call die in the first place?

Re: receiving email message with an attachment.
by kennethk (Monsignor) on Feb 27, 2013 at 15:55 UTC
    To expound upon Corion's point, you need to revise your conditional logic. What you probably mean is "If there is a body, then open it and output it." rather than "If there is no body, then bail." That could be written as
    if ($body = $part->bodyhandle) { $IO=$body->open("r"); print while(defined($_= $IO->getline)); }

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      what I mean is : Where is the body with the actual message? How do i get access to that? regards.

        Which module are you using? Have you tried reading its documentation? My guess is that you are using MIME::Parser and for your particular case, the first part has no body because it's a multipart. Have you tried checking the MIME type of your entity? See MIME::Entity#bodyhandle.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Where the body is depends on how the message is composed. Can you post a small but complete example of a message you are having trouble parsing?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020912]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-12-22 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls