Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
go ahead... be a heretic
 
PerlMonks  

Carving up MIME email on STDIN

by stew (Scribe)
on Dec 03, 2003 at 18:14 UTC ( #311961=perlquestion: print w/ replies, xml ) Need Help??
stew has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am trying to take a MIME email on STDIN and then extract any attatchment and re-assemble them into a fresh message (after I've performed a few tasks on the original irrelevant to this question)

Here is my code that takes a Mail::Internet message and parses it into parts using MIME::Parser. I can get the mime types no probs using $parts[$i]->mime_type but which method do I call to get the file name and data for each part. There seems to be little info in the docs.
use Mail::Internet; use MIME::Parser; $mail = new Mail::Internet \*STDIN; # Create new parser my $parser = new MIME::Parser; # Keep parsed message bodies in core $parser->output_to_core('ALL'); # Make the Mail::Internet message into an array @lines = (@{$mail->header}, "\n", @{$mail->body}); # Parse an in-core MIME message $parser->parse_data(\@lines); $ent->make_multipart; @parts = $ent->parts; foreach my $i ( 0 .. $#parts ) { print "Part $i ". $parts[$i]->mime_type . "\n\n"; }
(In the real app the print bit in the above loop would probably be used to add parts to MIME::Lite message or similar!)

Many thanks

Stew

Comment on Carving up MIME email on STDIN
Select or Download Code
Re: Carving up MIME email on STDIN
by thatguy (Parson) on Dec 03, 2003 at 19:25 UTC
    I was digging around the Super Search and found this node which would suggest that the following would work:

    ## snip foreach my $i ( 0 .. $#parts ) { ## print the mime type print "Part $i ". $parts[$i]->mime_type . "\n\n"; ## print the name print "Name $i ". $parts[$i]->head->mime_attr('content-disposition.f +ilename'); } ## snip

    keep in mind that this code is UNTESTED.

    -phill

Re: Carving up MIME email on STDIN
by markov (Scribe) on Dec 03, 2003 at 19:31 UTC

    Yes, there is little info in the docs -- and I have to maintain that MailTools code :-( -- Anyway, after playing around with it, I decided that the structure chosen is not sufficient for the current complex messages. Have you thought of multi-level multiparts? Anyway: there came MailBox

    Example to show that it works easy (although installation has too many dependencies, I know):

     use File::Temp;
     my $dir = tempdir; mkdir $dir or die;
     
     use Mail::Message;
     my $msg = Mail::Message->read(\*STDIN);
     foreach my $part ($msg->parts('RECURSE'))
     {  next if $part->isBinary;
        my $fn  = $message->body->dispositionFilename($dir);
    
        $message->decoded->write(filename => $fn)
            or die "Couldn't write to $fn: $!\n";
     }
    

    See Mail::Box in HTML. Have a look at rebuild(), which may be able to do for you what you had planned. Just an alternative to MIME::* and MailTools to keep in mind.

Re: Carving up MIME email on STDIN
by waswas-fng (Curate) on Dec 03, 2003 at 23:25 UTC
    Also you may want to be careful about the order of items as you recombine. Some of the ways email agents look for text vs html are based on ordering of the attachments. I know I have had problems with outlook and some other email clients not picking up the text or html version of an email correctly after rewriting before.


    -Waswas
Re: Carving up MIME email on STDIN
by stew (Scribe) on Dec 04, 2003 at 10:00 UTC
    Thanks for the help guys - looks like all the info I need.

    Stew

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://311961]
Approved by jdtoronto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2014-04-21 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls