Date: 26 Jun 2001 13:08:18 -0700
From: adsouza@globix.net (Anthony)
Subject: HELP! Parsing an Specific Attachement using MIME-tools-5.410 in Perl
Message-Id: <2ba4a7b9.0106261208.1532489c@posting.google.com>

Hello,

I am trying to have a perl script parse out a winmail.dat attachement
out of an email message using MIME::Parser.  However, upon trying
this, as the modules example, it is returning the pointer to the hash:
     MIME::Entity=HASH(0x41df38)

Now the string it has to match "winmail.dat" will be in a parts
attachement.  When the module splits the entity via parts:
     my @parts = $entity->parts;
i did not realize that this is a hash.  So i proceded to print out the
values of the hash:
     
     KEY *ME_Bodyhandle* -> VALUE *MIME::Body::File=HASH(0x421abc)*
     KEY *ME_Parts* -> VALUE *ARRAY(0x41de84)*
     KEY *mail_inet_head* -> VALUE *MIME::Head=HASH(0x41df44)*

And I was doing pattern matching on the value.  Yet it is another
pointer.

So, what I wish to do, is if there is a winmail.dat file, to ONLY
delete that attachment, if other attachements, leave them in the
parts.

Can this be done?  Any help would be greatly appreciated.  I have been
testing with the mimedump that comes with the perl module and have
hacked it to peices, yet can not seem to get what I need!  Please
Help!

Thanks a lot!!!
Anthony D.
System Architecture
Globix Corporation
adsouza@globix.net

---------------------------------------------------------------------------
My script looks something like this:
#!/usr/local/bin/perl -w

use MIME::Parser;
#------------------------------
# dump_entity ENTITY, NAME
# Recursive routine for dumping an entity.
sub dump_entity {
    my ($entity, $name) = @_;
    defined($name) or $name = "'anonymous'";
    my $IO;

    # Output the head:
    print "\n", '=' x 60, "\n";
    print "Message $name: ";
    print "\n", '=' x 60, "\n\n";
    print $entity->head->original_text;
    print "\n";

    # Output the body:
    my @parts = $entity->parts;
    if (@parts) {                     # multipart...
	my $i;
	foreach $i (0 .. $#parts) {       # dump each part...
	    print "\t\t\t\t\t>>$i<<\n";
	    foreach my $key (keys %{$parts[$i]})
	    {
			print "KEY *$key* VALUE *".%{$parts[$i]}->{$key}."*\n";
			if ($key eq "ME_Parts")
			{
				print "Found ME_Parts\n";
				my $temp = %{$parts[$i]}->{$key};
				foreach my $key1 (@$temp)
				{
					print "ARRAY VALUE *$key1*\n";
				}
			}
	    }
	    
	    #print "if (". %{$parts[$i]}->{""} ." !~ /letter2landlord.txt/i)
{\n";
	    if ($parts[$i] !~ /letter2landlord.txt/i) {
		    print "IN!!!!\n\n";
		    print "++$parts[$i]++\n";
	   	 dump_entity($parts[$i], ("$name, part ".(1+$i)));
	    }
	}
    }
    else {                            # single part...	

	# Get MIME type, and display accordingly...
	my ($type, $subtype) = split('/', $entity->head->mime_type);
	my $body = $entity->bodyhandle;
	if ($type =~ /^(text|message)$/) {     # text: display it...
	    if ($IO = $body->open("r")) {
		print $_ while (defined($_ = $IO->getline));
		$IO->close;
	    }
	    else {       # d'oh!
		print "$0: couldn't find/open '$name': $!";
	    }
	}
	else {                                 # binary: just summarize it...
	    my $path = $body->path;
	    my $size = ($path ? (-s $path) : '???');
	    print ">>> This is a non-text message, $size bytes long.\n";
	    print ">>> It is stored in ", ($path ? "'$path'" :
'core'),".\n\n";
	}
    }
    1;
}

#------------------------------
# main
sub main {
    print STDERR "(reading from stdin)\n" if (-t STDIN);

    # Create a new MIME parser:
    my $parser = new MIME::Parser;
    
    # Create and set the output directory:
    (-d "mimedump-tmp") or mkdir "mimedump-tmp",0755 or die "mkdir:
$!";
    (-w "mimedump-tmp") or die "can't write to directory";
    $parser->output_dir("mimedump-tmp");
    
    # Read the MIME message:
    $entity = $parser->read(\*STDIN) or die "couldn't parse MIME
stream";

	print ">>>>$entity\n\n\n";
    # Dump it out:
    dump_entity($entity);
}
exit(&main ? 0 : -1);

#------------------------------
1;
---------------------------------------------