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

Hello, I am having this strange issue where using MIME:Parser (my version is activestate 5.8.8), some mime emails containing an image are decoded perfectly, BUT some others are not !

Any help WOULD BE GREATLY APPRECIATED !!!

* Here the problematic email: www.biospring.com/email

* Here is the problematic image output, both what it should be, and what MIME::Parser does to the image: www.biospring.com/email/emailimages.html

* Here is the code (just want to extract the email text body and any images):

sub parse { use MIME::Parser; my $dir = "c:\\tmp\\parsedemails"; if (not -d $dir) { mkdir $dir or die "Can't create directory $dir: $!\n"; } ### Create a new parser object: my $parser = new MIME::Parser; ### Tell it where to put things: $parser->output_under($dir); ### Open the file: open my $fh, '<', $fileopen or die "Can't read $filopen: $!\n"; ### Parse an input filehandle: my $entity = $parser->parse($fh); # $entity->dump_skeleton; $path = "c:\\tmp\\parsedemails"; opendir(DIR, $path); @lines = readdir(DIR); closedir(DIR); ### the last directory name, is the name of the newly most recently cr +eated directory by Mail::Parser $directory = $lines[$#lines]; Print "DIR $directory END "; $pathdir = $path."\\".$directory; opendir(DIR, $pathdir) or die "can't opendir $pathdir: $!"; while (defined($file = readdir(DIR))) { push @files, $file; # Print " FILE $file END"."\n"; } closedir(DIR); } # end sub

Replies are listed 'Best First'.
Re: MIME: Parser image decode sometimes doesnt work
by oko1 (Deacon) on Oct 11, 2010 at 05:15 UTC

    The email at the link that you've provided above is incorrectly formatted; in fact, it's broken so badly that it's not recognized by any of the several email clients that I've tried. Just for kicks, I repaired its structure (mostly indenting the continued header and MIME definition lines, but also removing blank lines and adding the 'From ' header), fixed the case error in your script (there's no such function as "Print"), and declared the variables in your sub so that they'd work with "use strict" - something you should have been using all along - at which point, it all worked fine.

    Take-home message: a) Validate your data. b) Ensure that your code is vetted by both "use warnings" and "use strict". c) Pay careful attention to the error messages returned by the Perl parser, and fix them; this will take care of the majority of your problems.


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
      Thanks Oko ! Can you please post back the correctly formatted email out of curiousity ? Yah I cannot control it when machines send bad emails... thanks!
Re: MIME: Parser image decode sometimes doesnt work
by Khen1950fx (Canon) on Oct 11, 2010 at 00:13 UTC
    You forgot to declare some variables:). Fixed:
    #!/usr/bin/perl use strict; use warnings; use MIME::Parser; sub parse { use MIME::Parser; my $dir = 'c:\\tmp\\parsedemails'; if ( not -d $dir ) { die "Can't create directory ${dir}: $!\n" unless mkdir $dir; } my $fileopen; my $parser = new MIME::Parser; $parser->output_under($dir); die "Can't read ${fileopen}: $!\n" unless open my $fh , '<', $fileopen; my $entity = $parser->parse($fh); my $path = "c:\\tmp\\parsedemails"; opendir DIR, $path; my @lines = readdir(DIR); closedir DIR; my @files; my $directory = $lines[$#lines]; print "DIR $directory END "; my $pathdir = $path . "\\" . $directory; die "Can't opendir ${pathdir}: $!" unless opendir DIR, $pathdir; while (defined( my $file = readdir DIR )) { push @files, $file; } closedir DIR; }