Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found