in reply to Accessing IMAP-Server Outlook.office365.com via Perl
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.
|
|---|