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.
--Jim
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|