Here's a script where I've used Mime::Parser to strip attachments off of emails... Now, these are jpg's, sent from my phone, and the script actually creates a thumbnail and deposits the original plus thumbnail in separate directories...

Keep in mind, this code is not "production" level and therefore not in great shape, but it suffices for my purposes.

#!/usr/bin/perl -w use strict; use MIME::Parser; use MIME::Base64 qw(decode_base64); use Image::Magick::Thumbnail; my $imagedir = '/www/mainimagedir/photos'; my $archivedir = '/www/mainimagedir/photos/arch'; my $thumbdir = '/www/mainimagedir/photos/thumbs'; my $homepage_image = $imagedir . '/image.jpg'; my $archive_image = $archivedir . '/' . time . '.jpg'; my $homepage_thumb = $thumbdir . '/image_thumb.jpg'; my $archive_thumb = $thumbdir . '/' . time . '_thumb.jpg'; open LOG, ">>/www/mainimagedir/photos/photoblog.log"; print LOG "############## NEW RUN \@" . time . " ################\n"; print LOG "homepage_image: $homepage_image\n"; print LOG "archive_image : $archive_image\n"; print LOG "homepage_thumb: $homepage_thumb\n"; print LOG "archive_thumb : $archive_thumb\n"; my $parser = new MIME::Parser; $parser->output_to_core(1); print LOG "created parser\n"; my $message = $parser->parse(\*STDIN); print LOG "-=-=-=-=-=-=-=-=-=- message -=-=-=-=-=-=-=-=-=-\n$message\n +-=-=-=-=-=-=-=-=-=- end msg -=-=-=-=-=-=-=-=-=-\n"; foreach my $part ($message->parts_DFS) { print LOG "processing part\n"; if ($part->bodyhandle) { if ($part->mime_type eq 'image/jpeg') { print LOG "ok, it's a jpeg image we're dealing wit +h\n"; my $data = $part->as_string; print LOG "===================== image data == +===================\n$data\n===================== end image ======== +=============\n"; my @raw_part = split(/\n/, $data); my @edited_part; foreach my $line (@raw_part) { next if ($line =~ /^Content/); next if ($line =~ /^\s+$/); next if ($line =~ /^$/); next if ($line =~ /name/); next if ($line =~ /mode/); next if ($line =~ /x\-mac/); push @edited_part, $line; } $data = join("\n", @edited_part); print LOG "===================== image data == +===================\n$data\n===================== end image ======== +=============\n"; $data = decode_base64($data); open HOME, "> $homepage_image" or die "Couldn' +t open $homepage_image for write: $!\n"; print HOME $data; close HOME; print LOG "wrote homepage image\n"; open ARCHIVE, "> $archive_image" or die "Could +n't open $archive_image for write: $!\n"; print ARCHIVE $data; close ARCHIVE; print LOG "wrote archive image\n"; my $src = new Image::Magick; print LOG "created ImageMagick object\n"; $src->Read($homepage_image); print LOG "read homepage_image\n"; my ($thumb,$x,$y) = Image::Magick::Thumbnail:: +create($src,64); print LOG "created thumbnail\n"; $thumb->Write("$homepage_thumb"); $thumb->Write("$archive_thumb"); print LOG "wrote thumbnails\n"; } print LOG "processed image part\n"; } print LOG "tail end of some loop\n"; } close LOG;

The biggest thing I had problems with was the parsing out some of the mime headers, hence the ton 'o' logging, which is normally commented out during heavy use.



--chargrill
$/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );

In reply to Re: pull off PDF attachment by chargrill
in thread pull off PDF attachment by bobdole

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.