<?xml version="1.0" encoding="windows-1252"?>
<node id="193954" title="Re: Saving certain email attachements using MIME::Tools" created="2002-08-29 20:20:03" updated="2005-07-10 16:02:32">
<type id="11">
note</type>
<author id="95917">
jlongino</author>
<data>
<field name="doctext">
I believe this will do what you want.
&lt;code&gt;#!/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-&gt;output_dir($outputdir);

## this creates files for all parts
my $entity = $parser-&gt;read(\*STDIN);
my $num_parts = $entity-&gt;parts;

## define attachment types to keep -- notice all lowercase
## add as many types as you like.
my %type_ok = (
   'application/octet-stream' =&gt; 1
);

if ($num_parts &gt; 0) {
    for (my $i = 0; $i &lt; $num_parts; $i++) {
        my $part = $entity-&gt;parts($i);
        ## note: lc so types have a better chance of matching
        my $type = lc $part-&gt;mime_type;
        my $bh = $part-&gt;bodyhandle;
        ## delete parts we don't want
        if (! exists $type_ok{$type}) {
           my $status = system("rm '$bh-&gt;{MB_Path}'");
           die $! unless $status == 0;
        }
    }
}&lt;/code&gt;
The program is executed like this:
&lt;p&gt;&lt;code&gt;program &lt; single.message.mime&lt;/code&gt;
&lt;p&gt;&lt;b&gt;Update&lt;/b&gt;: './mimemail' directory should already exist. Also removed unneeded 'my $msg_body;' line.

&lt;p&gt;--Jim</field>
<field name="root_node">
193909</field>
<field name="parent_node">
193909</field>
</data>
</node>
