http://qs321.pair.com?node_id=11141594

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

Deer Monkees

I want to retrieve emails from remote server which supports IMAP (I guess POP3 too). I want to search for messages by subject (or content if that's easy), or just get the unread messages or get the N most recent messages from folder. After downloading the emails I want to parse them because they can contain attachments (images, pdf etc.). And then save them attachments to disk. And mark the message on server as 'read' or even delete it.

I have been searching for a couple of hours and found that Mail::IMAPClient is recently updated but I failed to tell it to give me the message in a format that Email::MIME can parse. OTOH Net::IMAP::Client worked without too much hassle and Email::MIME seems to be able to parse its fetched messages but I don't know how to save email and its attachments to disk. And it's not recently updated.

To summarise: I am open to using any module(s) which can fetch me my emails (searching for 'unread' is great) parse them, give me subject, content, date and offer me a simple way to save attachments to disk preferably with their original filename. Any suggestions AND example code?

I can offer this snippet for Net::IMAP::Client and Email::MIME which seem to work OK but don't know how to save:

use Net::IMAP::Client; use Email::MIME; use Data::Dumper; my $imap = Net::IMAP::Client->new( server => 'xyz.com', user => 'xxx', pass => 'xxx', ssl => 0, port => 143, ); die "failed to instantiate $@." unless defined $imap; $imap->login or die "Could not connect: ".$imap->last_error."\n"; my @folders = $imap->folders or die "List folders error: ", $imap->last_error, "\n"; print "Folders: @folders\n"; # get total # of messages, # of unseen messages etc. (fast!) my $status = $imap->status(@folders); # hash ref! print Dumper($status); $imap->select('INBOX') or die "Select 'INBOX' error: ", $imap->last_error, "\n"; # do a reverse-date search (most recent first) my $messages = $imap->search('ALL', '^DATE'); for my $amid (@$messages){ print "message id: $amid\n"; my $msg = $imap->get_rfc822_body($amid); my $parsed = Email::MIME->new($msg); die "failed to parse" unless $parsed; my @parts = $parsed->parts; # These will be Email::MIME objects, t +oo. my $decoded = $parsed->body; my $non_decoded = $parsed->body_raw; for my $apart (@parts){ # indeed they are Email::MIME, how do I save them??? print "got this email part: $apart\n" } my $content_type = $parsed->content_type; last; } $imap->logout();

and this to get me started with Mail::IMAPClient

use Mail::IMAPClient; use Email::MIME; use Data::Dumper; my $imap = Mail::IMAPClient->new( Server => 'abc.com', User => 'xxx', Password => 'xxx', Ssl => 1, Uid => 1, # Starttls => 1, ); die "failed to instantiate." unless defined $imap; $imap->connect or die "Could not connect: $@\n"; my $folders = $imap->folders or die "List folders error: ", $imap->LastError, "\n"; print "Folders: @$folders\n"; $imap->select( 'INBOX' ) or die "Select 'INBOX' error: ", $imap->LastError, "\n"; my @messages = $imap->messages; my $msg = pop @messages; my $obj = $imap->get_bodystructure($msg); print Dumper($obj);

bw, bliako