[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Decode email message

Posted on 2001-07-05
6
Medium Priority
?
200 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

656 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