?
Solved

Decode email message

Posted on 2001-07-05
6
Medium Priority
?
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

 
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 1000 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

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

752 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