sebastiannielsen has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use MIME::Parser; srand; $parser = new MIME::Parser; $foldername = time().int(rand(10)).int(rand(10)).int(rand(10)).int(ran +d(10)).int(rand(10)); mkdir("/var/smtp/my/recv/".$foldername); $parser->output_dir("/var/smtp/my/recv/".$foldername); $parser->tmp_dir("/var/smtp/tmp"); eval{$parser->parse(\*STDIN);};
It works very well, EXCEPT for mails which contain inline attachments. I need to get the CID of these attachments. It should work "unlimited" deep into the hiearchy, so even if the mail contains a another mail which contains a third mail which contains... and so on and so on it should work.
The attachments are printed with their original filenames. Is there any way to:
-either get the MIME::Parser's filename of a inline attachment with the CID (like "1290910643.25733.0.camel@sebastian-desktop", stored in a variable) converted to the filename (like "image.png").
-or get a list of all CIDs found in a mail, and with their corresponding filenames "invented" by MIME::Parser.
-or be able to input a filename created by MIME::Parser, and get it corresponding CID.
The best would be the second option, if I could get a list of all CIDs found in a MIME mail, with their corresponding filenames "created" by MIME::Parser. Note that MIME::Parser creates a file with the name + a -1 like image-1.png, if theres multiple files with the same name. This needs also to be taken in consideration.
I can then write this list into a text file placed in the mail folder for the webmail system to pick up, for it to replace the CIDs in the IMG tags with their corresponding correct paths to the images.
I also would want to get a list of the MIME content types for all the attachments which MIME::Parser writes along with their MIME::Parser filenames.
If you wonder, im building a webmail system in perl which does NOT go through the POP3, instead it goes straight from SMTP server into the webmail folder, and it is parsed and complete so the only thing the webmail system has to do is to display the mails and eventual attachments.
******************************************************************
Solved
******************************************************************
Solved it now thanks to Anonymous Monk. The script now looks like this, and it works very well:
#!/usr/bin/perl use MIME::Parser; use MIME::Base64; srand; $parser = new MIME::Parser; $foldername = time().int(rand(10)).int(rand(10)).int(rand(10)).int(ran +d(10)).int(rand(10)); mkdir("/var/smtp/my/recv/".$foldername); $parser->output_dir("/var/smtp/my/recv/".$foldername); $parser->tmp_dir("/var/smtp/tmp"); $returnentity = eval{$parser->parse(\*STDIN);}; @writetofile = dump_entity($returnentity); open(INDEXFILE, ">/var/smtp/my/recv/".$foldername."/mailindex.txt"); flock(INDEXFILE,2); print INDEXFILE @writetofile; close(INDEXFILE); sub dump_entity { my ($entity) = @_; my @filedata = (); my @parts = $entity->parts; if (@parts) { my $i; foreach $i (0 .. $#parts) { push(@filedata, dump_entity($parts[$i])); } } else { my $filepath = $entity->bodyhandle->path; $filepath =~ s/^(.*)\/([^\/]*)$/$2/si; $filepath = encode_base64($filepath); $filepath =~ s/\n//sgi; $filepath =~ s/\r//sgi; $filepath =~ s/\t//sgi; my $fileid = encode_base64($entity->head->get('content-id')); $fileid =~ s/\n//sgi; $fileid =~ s/\r//sgi; $fileid =~ s/\t//sgi; my $filetype = encode_base64($entity->head->mime_type); $filetype =~ s/\n//sgi; $filetype =~ s/\r//sgi; $filetype =~ s/\t//sgi; push(@filedata, $filepath."::".$fileid."::".$filetype."\n"); } return @filedata; }
I get the attachments as binary files in $foldername, a convient file called mailindex.txt in the $foldername with the following content:
Decoded:bXNnLTMyNTUwLTEudHh0::::dGV4dC9wbGFpbg== bXNnLTMyNTUwLTIuaHRtbA==::::dGV4dC9odG1s Ym90LnBuZw==::PDEyOTA5ODUxNTMuMzIxNDMuMC5jYW1lbEBzZWJhc3RpYW4tZGVza3Rv +cD4K::aW1hZ2UvcG5n
msg-32550-2.html looks like this:msg-32550-1.txt :: :: text/plain msg-32550-2.html :: :: text/html bot.png :: <1290985153.32143.0.camel@sebastian-desktop> :: image/png
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN"> <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8"> <META NAME="GENERATOR" CONTENT="GtkHTML/3.28.1"> </HEAD> <BODY> sön 2010-11-28 klockan 23:59 +0100 skrev sebastian:<BR> <BLOCKQUOTE TYPE=CITE> <IMG SRC="cid:1290985153.32143.0.camel@sebastian-desktop" ALIGN="b +ottom" BORDER="0"><BR> </BLOCKQUOTE> test </BODY> </HTML>
As you see, it will be very easy to replace the "cid:1290985153.32143.0.camel@sebastian-desktop" src with something like http://www.mydomain.com/webmail/getattachment.cgi?attachment=bot.png
Everything solved.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Get CID inline attachments with MIME::Parser
by afoken (Chancellor) on Nov 28, 2010 at 08:15 UTC | |
by sebastiannielsen (Initiate) on Nov 28, 2010 at 20:58 UTC | |
by roboticus (Chancellor) on Nov 28, 2010 at 21:17 UTC | |
by Marshall (Canon) on Nov 28, 2010 at 23:24 UTC | |
by afoken (Chancellor) on Nov 29, 2010 at 13:35 UTC | |
|
Re: Get CID inline attachments with MIME::Parser
by Anonymous Monk on Nov 28, 2010 at 04:24 UTC | |
by sebastiannielsen (Initiate) on Nov 28, 2010 at 23:29 UTC | |
|
Re: Get CID inline attachments with MIME::Parser
by Anonymous Monk on Nov 28, 2010 at 04:26 UTC |