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

I'm using Mail::Box and friends to find all the image/jpeg bodies in an mbox file.

Does anyone know of a handy way to take such a body and write it to a .jpg file? I have written it out as text and was going to use C for the .jpg, but looking at the original data in the mbox I'm realizing this is not a binary stream such as you would get with a normal HTTP response -- the values are all valid ascii, ie. this is not normative jpeg data.

Strangely, I can't find anything out about that googling either. Anyone know anything?

SOLVED
Okay, it's encoded in base64 -- for posterity:
use Mail::Box::Manager; use MIME::Base64::Perl; [...] my $msg = $folder->message(0); if ($msg->isMultipart) { my @parts = $msg->parts; foreach (@parts) { if ($_->contentType eq "image/jpeg") { (my $body = $_->body) =~ s/\n//g; my $jpg = decode_base64($body); } } }
$jpg can be written straight to a .jpg file.

Whew!

Replies are listed 'Best First'.
Re: grabbing jpg from mbox
by zwon (Abbot) on Jul 30, 2009 at 18:58 UTC

    Use Email::MIME to parse a message and get array of message parts, then for every part check its content type and if it's 'image/jpeg' save into file.

    Update: concerning ascii data -- it's a base64 encoded content, the body method from Email::MIME should automatically decode it.

      Vis. the base64 bit thanks! We now have two ways to do this here.