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.
$/ = 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |