Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Actually I have some production code that does this, with Office365 even. I've been meaning to clean it up a bit to be less work-specific and publish it on CPAN but never got around to it. But, here's some snippets that might help you:
my $conn= Net::IMAP::Simple->new( $host, port => $port, ($port == 993? ( use_ssl => 1, ssl_options => [ SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER() ] ) : ()), debug => $log->is_trace, ) or _logcroak("Cannot connect to server $host: $Net::IMAP::Simple +::errstr"); if ($port != 993) { $conn->starttls or _logcroak("Can't start TLS after conencting to $host port +$port: ".$conn->errstr); } $conn->login($user, $self->login_pass) or _logcroak("Can't authenticate as $user to server $host: ".$co +nn->errstr); $log->info("Logged in as ".$user); my $mbox= $url->path; $mbox= 'INBOX' unless defined $mbox and length $mbox and $mbox ne ' +/'; $conn->select($mbox) or _logcroak("Can't select folder $mbox");

Then, in a loop:

$conn->select; # call select again to refresh message list
$msg_ids= [ $conn->search($imap_query) ];
for my $msgid (@$msglist) { $log->debug("Msg $msgid"); my $header_lines= $conn->top($msgid); my $head= MIME::Head->from_file(\join('', @$header_lines)) or di +e; ... # filtering code goes here, which inspects headers
# this is the code that pulls in the whole message my $fh= $conn->getfh($msgid) || \ _logcroak("Failed to get message file handle: ".$conn->errstr); my $p= MIME::Parser->new; $p->output_to_core(1); return $p->parse($fh);

I'll add that Email::MIME has a nicer interface than MIME::Parser but I had some particular edge cases where MIME::Parser worked better for me. I recommend starting with Email::MIME and see if it works for you.

The part where you want to search only unread messages is done using the IMAP query, and IMAP::Simple describes that well enough. Also I think Office365 doesn't support IMAP unless you specifically enable it somewhere in your account preferences.

In reply to Re: Accessing IMAP-Server via Perl by NERDVANA
in thread Accessing IMAP-Server via Perl by PerlingAround

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (4)
    As of 2019-11-19 05:04 GMT
    Find Nodes?
      Voting Booth?
      Strict and warnings: which comes first?

      Results (94 votes). Check out past polls.