Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Modifying MIME messages with MIME-Tools

by skazat (Chaplain)
on Nov 08, 2004 at 04:48 UTC ( [id://405973]=perlquestion: print w/replies, xml ) Need Help??

skazat has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I though I'd post this before I (well continue) to get my feet dirty:

I'm currently in need to read in an email message that will have a varied MIME structure. I want to modify some of the MIME parts - for instance, add a footer to any plain text versions, remove attachments, etc. Doing this with MIME-Tools, something I'm familiar with - doesn't seem super straightforward.

There is a module called, Email::MIME::Modifier, which seems to do exactly what I need, the only problem is that its prereqs list is very large and it's another tool that's similar to MIME-Tools, but isn't exactly MIME-Tools, thus my code is going to get unweildy.

Are there any code snippets out there that could lead me in the right direction? My main idea would be the recursively go through each MIME part and edit them in place, although the MIME::Entity/MIME::Parser docs on how to remove a part (let alone modify) aren't super clear.

I actually haven't even found a mailing list devoted to MIME-Tools.

Any bones to throw, oh fell monks?

 

-justin simoni
!skazat!

  • Comment on Modifying MIME messages with MIME-Tools

Replies are listed 'Best First'.
Re: Modifying MIME messages with MIME-Tools
by rob_au (Abbot) on Nov 08, 2004 at 10:49 UTC

    Modifying MIME components with MIME-Tools is not at all impossible - Although I cannot copy and paste code directly from existing code in which I have implemented this functionality using MIME-Tools (as it corporate code), I should be able to provide some example code (see below) to help you in your task.

    # The variable $entity contains a MIME::Entity object returned from +MIME::Parser # and from this object, a MIME::Body object is retrieved for content + manipulation. my $body = $entity->body; my $content = $body->as_string; # The content of the MIME component can now be modified as you see f +it $content =~ s/foo/bar/g; # The updated content must now be written back into the MIME message + component - # This is performed through manipulation of the MIME::Body object. my $io = $body->open('w'); $io->print( $content ) $io->close; # At this point, you may need to re-synchronise the headers of the M +IME::Entity # object - This method will add or update a Content-Length header wi +thin the # MIME component. $entity->sync_headers( 'Length' => 'COMPUTE', 'Nonstandard' => 'ERASE' );

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001011110000'"

      Rob,

      Thanks for given me that to go on! I think I've cracked it! I did have to tweak your code to the following to make it work with my setup:

      # The variable $entity contains a MIME::Entity object returned from +MIME::Parser # and from this object, a MIME::Body object is retrieved for content + manipulation. my $body = $entity->bodyhandle; my $content = $body->as_string; # The content of the MIME component can now be modified as you see f +it $content =~ s/foo/bar/g; # The updated content must now be written back into the MIME message + component - # This is performed through manipulation of the MIME::Body object. my $io = $body->open('w'); $io->print( $content ) $io->close; # At this point, you may need to re-synchronise the headers of the M +IME::Entity # object - This method will add or update a Content-Length header wi +thin the # MIME component. $entity->sync_headers( 'Length' => 'COMPUTE', 'Nonstandard' => 'ERASE' );

      basically, the line that says,

      my $body = $entity->body; Needed to be:

      my $body = $entity->bodyhandle;

       

      -justin simoni
      !skazat!

      OK!

      Here's my Proof of Concept to edit specific MIME types in a MIME Message. This example will only tweak parts that are in text/plain. How to exactly edit the mime part was graciously given by rob_au, all that had to after that was to create a recursive subroutine that would then reconstruct everything tweaked!

      #!/usr/bin/perl -w use strict; use MIME::Parser; use MIME::Entity; my $parser = new MIME::Parser; $parser->output_to_core(1); #so here's our test MIME thingy: my $test = MIME::Entity->build(Type => "multipart/alternative", From => 'me@myhost.com', To => 'you@yourhost.com', Subject => "Test Test Test"); $test->attach(Type => "text/plain", Data => "This is the plain text stuff" ); $test->attach(Type => "text/html", Data => "<h1>This is the HTML stuff</h1>" ); # Let's print the, "Before" print $test->as_string(); $test = tweak_plain_text($test); print "\n\nAnd After...\n\n\n"; print $test->as_string(); sub tweak_plain_text { my $entity = shift; my @parts = $entity->parts; if(@parts){ my $i; foreach $i (0 .. $#parts) { $parts[$i]= tweak_plain_text($parts[$i]); } $entity->sync_headers('Length' => 'COMPUTE', 'Nonstandard' => 'ERASE'); }else{ if($entity->head->mime_type eq 'text/plain'){ my $body = $entity->bodyhandle; my $content = $body->as_string; $content .= "\nThis has been tweaked!!!!\n"; my $io = $body->open('w'); $io->print( $content ); $io->close; $entity->sync_headers('Length' => 'COMPUTE', 'Nonstandard' => 'ERASE'); } return $entity; } my $content = $entity->as_string; return $entity; }

      Thanks everyone for the help!

       

      -justin simoni
      !skazat!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://405973]
Approved by sgifford
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found