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


In reply to How to get started with scraping my IMAP emails by bliako

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.