Solved

Decode email message

Posted on 2001-07-05
6
188 Views
Last Modified: 2013-12-25
Hi,

I have written a Perl program to retrieve email messages the only problem is decoding the message. I simply want the Body of the message, any attachments and the header values in a hash.

Thanks in advance,
Steve
0
Comment
Question by:shodgkiss
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:andreif
ID: 6258961
Hi,
here is a solution
------------------

use MIME::Parser;

$parser = new MIME::Parser;
$parser->output_dir("/tmp");

$entity = $parser->read(\*STDIN) or die "Couldn't parse MIME stream";

------------------

After that you can use    
$entity->head - for message header
$entity->bodyhandle - for message body
$entity->parts - for parts of multipart message (attachments)

Check manuals for
MIME::Parser
There are nice examples

0
 

Author Comment

by:shodgkiss
ID: 6259103
andreif,

Before i give you the points could you give me an example of placing the attachments to a file on the server?

Thanks a lot
Steve
0
 

Author Comment

by:shodgkiss
ID: 6259109
oh and i have the message in a variable on the server called $message, how do you use that instead of STDIN??
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Expert Comment

by:andreif
ID: 6259424
Hi Shodgkiss,

use

$entity = $parser->parse_data($message);

to parse data from variable

All attached files will stored in the directory, specified with
$parser->output_dir("/tmp");

(in my example /tmp, of course)
 
you can see the structure of the entity using
print $entity->dump_skeleton;

to see attached files use array $entity->parts
every entry is also an entity. The entity object has useful methods like

$entity->mime_type
$entity->path

Check documentation for MIME::Entity module for the full list of properties
0
 
LVL 6

Expert Comment

by:andreif
ID: 6259449
So, your code can be like this

use MIME::Parser;

$parser = new MIME::Parser;
$parser->output_dir("/tmp");

$entity = $parser->parse_data($message);

# do something with $entity->head; $entity->bodyhandle

foreach $ent ($entity->parts) {
  # do something with $ent->path; $ent->bodyhandle;
}

0
 
LVL 6

Accepted Solution

by:
andreif earned 250 total points
ID: 6277112
So, your code can be like this

use MIME::Parser;

$parser = new MIME::Parser;
$parser->output_dir("/tmp");

$entity = $parser->parse_data($message);

# do something with $entity->head; $entity->bodyhandle

foreach $ent ($entity->parts) {
 # do something with $ent->path; $ent->bodyhandle;
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now