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

Hi Monks. I'm using MIME::Parser to parse some emails. I'm trying to get the attached images and all works well until I came across .mac webmail. A mail comes which looks like this
--Apple-Mail-30--196590585 Content-Disposition: inline; filename=moriyama1.gif Content-Transfer-Encoding: base64 Content-Type: application/applefile; name="moriyama1.gif" AAUWBwACAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAJAAAAMgAAAAoAAAADAAAAPAAAAA1HSU +ZmcHJ2 dwEAbW9yaXlhbWExLmdpZg== --Apple-Mail-30--196590585 Content-Disposition: inline; filename=moriyama1.gif Content-Transfer-Encoding: base64 Content-Type: image/gif; x-mac-creator=70727677; x-unix-mode=0644; x-mac-type=47494666; name="moriyama1.gif" R0lGODlhZABnALMAAPDv79LR0FJQT66trJGQjykoJ9zb2m9ubebl5aCfnbu5uMfFxIKBfw +EBAQIC AgAAACH5BAA [etc...] --Apple-Mail-30--196590585--

Here the parser chokes on the image. Since I have never seen any of the other images with two headers like this, can I assume it is because the mail software is formatting it strangly? If so how can I get the real image which starts with the Content-Type: image/gif ?

My code looks like this. (I know this is not pretty, and I'm open to sugegstions to make it nice, but the thing I really care abotu is just getting the image now)

foreach my $part (@parts) { next unless defined $part->head; my $head_string = $part->head->as_string; next unless $head_string =~ m!text/plain|alternative|image|oct +et|gif!i; my $body_string = $part->bodyhandle->as_string; my @part = ($head_string, $body_string); push(@text, \@part) if $head_string =~ m!text/plain|altern +ative!i; push(@images, \@part) if $head_string =~ m!image|octet|gif +!i; } $thing->{'images'} = \@images; $thing->{'text'} = \@text; return($thing); }

If I don't have the next statement in there, it dies when the parser failes on thje "application/applefile" header. If I put in the next statement, it skips that header, but also skips the images.

Is there anything I can do about this without having to ditch MIME::Parser (I really really really want it to work with MIME::Parser)

K

Replies are listed 'Best First'.
Re: MIME::Parser and applefile
by Corion (Patriarch) on Jul 07, 2003 at 17:07 UTC

    My completely uninformed guess is that the first attachment is what Mac people call the "Resource Fork" of that file, while the second attachment contains the actual data of the image. So you would be reasonably safe to throw the resource fork away (if you can). The resource fork is always a problem when transferring files with such metadata through media that does not adequately support this idea...

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: MIME::Parser and applefile
by Thelonius (Priest) on Jul 07, 2003 at 20:09 UTC
    I would suggest doing your match on the $part->effective_type rather than on the whole $head->as_string.

    I'm not sure what you mean by the parser failing. Is that some other part of your program? This part works fine for me. I'd recommend structuring your loop more like this:

    foreach my $part (@parts) { next unless defined $part->head; my $type = $part->effective_type; my $filename = $part->head->recommended_filename || ""; if ($type =~ m!application/applefile!i) { # print STDERR "skipping: $type $filename\n"; next; } my @thispart = ($part->head->as_string, $part->bodyhandle->as_string); if ($type =~ /^image/i || ($type =~ /application|octet/i && $filename =~ /\.gif$/i)) { push @images, \@thispart; } elsif ($type =~ /^text/i) { push @text, \@thispart; } }
      Thanks. I havn't tried it yet, but it+ looks nice! and I'm always looking for something to make the crappy code better.
Re: MIME::Parser and applefile
by demerphq (Chancellor) on Jul 07, 2003 at 17:52 UTC

    Im with Corion on this one. The first is an attachment you can ignore outright (Content-Type: application/applefile;) the second is the GIF propper.


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: MIME::Parser and applefile
by bobn (Chaplain) on Jul 07, 2003 at 20:16 UTC

    If I put in the next statement, it skips that header, but also skips the images.

    Why? 'gif' exists in both headers, does it not?

    --Bob Niederman, http://bob-n.com