[Posted and mailed]
Quote:
> Thanks Ian. The module seems to do alot. I am having trouble figuring
> out how to get access to the body. I am forwarding the emails to my script
> which MIME is parsing.
> my $parser = new MIME::Parser;
> mkdir("/tmp/$$",0755);
> $parser->output_dir("/tmp/$$");
> $entity = $parser->read(\*STDIN) || die "couldn't parse MIME stream";
> $entity->dump_skeleton;
Yep I do somrthing very similar to this maybe even exactly the same, I have
wrapped it up in a module so deciphering it to show you only the relevant
part is fun. (The module is an wrapper around various modules in MIME-Tools
which I use to encode an decode messages giving me output in the form I want
it. Yes this includes an array of parts so a count is easy) The following
is very similar to the above: (be kind I am still learning)
[not included:- the bits that set up these variables]
my $entity = {
USER => $user,
TYPE => {%TypeFor},
DIR => $dir,
ENT => undef,
FILES => [],
};
my $obj = bless $entity, $self;
if ($hdr) {
my $msg = $hdr."\n".$bdy;
(-d $dir) or mkdir $dir,0755 or
die "Can't create directory $dir: $!";
my $parser = new MIME::Parser;
$parser->output_to_core($size); # size should be 0, I set it earlier
$parser->output_dir($dir) if $dir;
$obj->{ENT} = $parser->parse_data($msg);
}
[there is more to encode mime messages]
return $obj;
As you can see I use the parser->parse_data call to get a decoded entity.
Quote:
> The method dump_skeleton works great. I am assuming that the
> text of the body will be writen to a filename "msg-<dir name>-1.doc".
> I am not sure what "1" is suppose to represent in the filename.
> The method also dumps the number of files it created. I see this
> reference as "Num-parts: #". I could not find a method which
> I can call to check for the total numbers of files it will output.
> What would be helpful would be an array of files it created.
> Once I have the body handle I can then print it out only if it does
> not contain attachments.
> $bodyh = $entity->bodyhandle;
> $IO = $bodyh->open("r") || die "open body: $!";
> while (defined ($_ = $IO->getline)) {
> print $_;
> }
> $IO->close;
> This does not work when there are attachments. Do you have problems
> with this? I keep getting the message :
> Can't call method "open" without a package or object reference at mime.pl line
> 63, <STDIN> chunk 245.
This is where things really start to differ, the folowing 2 routines are
defined in the module
sub _DUMP {
# INTERNAL SUBROUTINE DO NOT USE
# recursively retrieve entity part file names and MIME types
my $self = shift;
my $ent = shift || $self->{ENT};
# Output the body:
}
else { # it's single part
# get file name and the MIME type
my ($name,$path,$suffix) = fileparse($ent->bodyhandle->path, '\..*');
my $type = $ent->head->get('Content-Type');
($type,undef) = split ';', $type, 2;
my $enc = $ent->head->get('Content-Transfer-Encoding');
$enc = $enc ? $enc : '7-bit'; # assume 7-bit for all 'message' parts
$type =~ tr/\s//; # where did the spaces come from? they are gone now.
return $name.$suffix.'/'.$type.'/'.$enc;
}
Quote:
}
I got the idea for how to do the above bit from the mimedump example script
that comes with MIME-Tools, or the bit that does the work anyway. (Thanks
Andreas and Eryq, afterall credit where credit is due).
sub dump_parts {
my $self = shift;
my $entity = shift || $self->{ENT};
Quote:
}
To hang it all together I do something like this:
my $ent = new Attach Header => $msg_hdr,
Body => $msg_bdy,
Dir => $wkg_dir,
MTypePath => $$stp{mime};
which will give me an array of elements something like this:
msg--1272-1.doc/text/plain/7bit
Which when lobbed through a conversion Module returns a parsed string
suitable for displaying in a web browser:
probably a lot of code that I have missed out, for brevity, and also
much that is not needed, but hey this is my first attempt and I am
learning a hell of a lot about Perl :-).
--
--
Ian J. Garlick
Children are unpredictable. You never know what inconsistency they're
going to catch you in next.
-- Franklin P. Jones