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

Sadly, there must be a dozen other threads on this but I still can't figure it out. In fact, I'm ripping off another thread, but they left off the secret sauce I need.

I email myself photos from my phone to my gmail account. I'm trying to setup a script that will run every few minutes, look for new photos, and if found copy the images from the mail message and into a folder. Really straight forward, it will always be an image file (maybe a few in the same message?), nothing too fancy. But I can't figure out mime parsing to save my life.

The part I need help with is at the very bottom, I only put the rest of the code in incase anyone needed the backdrop. Any chance anyone can fill in the missing chunk at the bottom where it says Attachment Parse Here?
use strict; use warnings; use MIME::Parser; use Mail::IMAPClient; use Data::Dumper; use IO::Socket::SSL; # Connect to the IMAP server via SSL my $socket = IO::Socket::SSL->new( PeerAddr => 'imap.gmail.com', PeerPort => 993, ) or die "socket(): $@"; # Build up a client attached to the SSL socket. # Login is automatic as usual when we provide User and Password my $client = Mail::IMAPClient->new( Socket => $socket, User => 'user', Password => 'pass', IgnoreSizeErrors => '1' ) or die "new(): $@"; if ($client->IsAuthenticated()) { my $msgct; my $seqno = 0; $client->select("INBOX"); $msgct = $client->unseen_count||'0'; print "Number of unread Messages: $msgct\n"; my @unread = $client->unseen or warn "Could not find unseen msgs: +$@\n"; foreach $seqno (@unread){ my $parser = MIME::Parser->new; my $entity = $parser->parse_data($client->message_string($seqn +o)); my $header = $entity->head; my $from = $header->get("From"); my $msg_id = $header->get("message-id"); my $to = $header->get_all("To"); my $date = $header->get("date"); my $subject = $header->get("subject"); print "Message-id: $msg_id"; print "From: ". $from; print "To: $to"; print "Date: $date"; print "Subject: $subject"; ###### Attachment Parse Here # my $content = get_msg_content($entity); $entity->purge(); # print "Content: $content"; }

Replies are listed 'Best First'.
Re: Attachment Extraction from Imap
by Anonymous Monk on May 14, 2012 at 05:52 UTC