Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Accessing IMAP-Server Outlook.office365.com via Perl

by NERDVANA (Acolyte)
on Sep 17, 2019 at 23:16 UTC ( #11106328=note: print w/replies, xml ) Need Help??


in reply to Accessing IMAP-Server Outlook.office365.com via Perl

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11106328]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2019-11-19 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (96 votes). Check out past polls.

    Notices?