Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Saving certain email attachements using MIME::Tools

by neilwatson (Priest)
on Aug 29, 2002 at 20:54 UTC ( #193909=perlquestion: print w/replies, xml ) Need Help??

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

Brothers and Sisters,

I'm creating a script that will save certain email attachments and ignore others:

my @attypes= qw(application/gzip application/x-tgz application/zip application/bzip2 ); my ($x, @attachment, $attachment, @attname); SNIP foreach $x (@attypes){ print "x = $x subentity =".$subentity->mime_type."\n"; if ($subentity->mime_type =~ m/$x/i){ $attachment = join "", @{$subentity->body}; push @attachment, $attachment; print $subentity->head->get('Filename')."\n"; if ($subentity->mime_type =~ m/filename="(.*)"/i){ push @attname, $1; last; } }

The prints are for debugging. Question, I can figure our the mime-type but how do I get the filename and the contents of that file?

Thank you,

Neil Watson

Replies are listed 'Best First'.
Re: Saving certain email attachements using MIME::Tools
by jlongino (Parson) on Aug 30, 2002 at 00:20 UTC
    I believe this will do what you want.
    #!/usr/local/bin/perl use MIME::Parser; use strict; use warnings; ## directory to store all parsed MIME parts my $outputdir = "./mimemail"; my $parser = new MIME::Parser; $parser->output_dir($outputdir); ## this creates files for all parts my $entity = $parser->read(\*STDIN); my $num_parts = $entity->parts; ## define attachment types to keep -- notice all lowercase ## add as many types as you like. my %type_ok = ( 'application/octet-stream' => 1 ); if ($num_parts > 0) { for (my $i = 0; $i < $num_parts; $i++) { my $part = $entity->parts($i); ## note: lc so types have a better chance of matching my $type = lc $part->mime_type; my $bh = $part->bodyhandle; ## delete parts we don't want if (! exists $type_ok{$type}) { my $status = system("rm '$bh->{MB_Path}'"); die $! unless $status == 0; } } }
    The program is executed like this:

    program < single.message.mime

    Update: './mimemail' directory should already exist. Also removed unneeded 'my $msg_body;' line.


Re: Saving certain email attachements using MIME::Tools
by blahblahblah (Priest) on Aug 30, 2002 at 00:04 UTC
    I think this would work:
    $body = $entity->bodyhandle; $filename = $body->path;
      $body returns: MIME::Body::InCore=HASH(0x83a39ac) while $filename comes out blank.


      foreach $x (@attypes){ if ($subentity->mime_type =~ m/$x/i){ $attachment = $subentity->bodyhandle->print(); push @attachment, $attachment; print "x = $x subentity =".$subentity->mime_type."\n"; push @attname, $subentity->head->mime_attr('content-disposition. +filename'); } }

      The attachment prints out but is not stored in $attachment. @attname now holds the attachment name correctly. All that remains is figuring out how to store the attachement into a variable for later use.

      Neil Watson

        You're setting $attachment to the return value of the print() method. The perldoc for MIME:Body doesn't say anything about print() returning anything. There are other methods that look like they'll do what you want, like as_string() and as_lines().

        MIME::Tools has some of the better documentation I've seen for modules, with lots of examples. You should run "perldoc MIME::Body" or look at these pages on

Re: Saving certain email attachements using MIME::Tools
by neilwatson (Priest) on Sep 05, 2002 at 17:07 UTC
Re: Saving certain email attachements using MIME::Tools
by Anonymous Monk on Aug 30, 2002 at 21:35 UTC
    You might need MIME::Parser

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2022-05-20 14:24 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (73 votes). Check out past polls.