user3601664
user3601664

Reputation: 21

PHP - Office365 connecting with imap_open

thanks for the anticipated help.

Does anyone know the current state of php connecting to office365 using imap_open?

I have tried all the suggestions found on stack overflow and my current code is below. I simply cant seem to connect to office365 using imap_open, either straight connection failure or other erros below.

Does anyone have this working in 2022

I have tried the code below which results in the error below. I have also gone through the office365 admin panel and whitelisted my server IP, i have enabled SMTP auth and enabled IMAP on the account I'm connecting to.

$username = 'xxxxx';
$password = 'xxxxx';

$host = "{outlook.office365.com:993/imap/ssl/novalidate-cert}Inbox";
$mbox=imap_open($host,$username,$password, NULL, 1, 
                           array('DISABLE_AUTHENTICATOR' => 'PLAIN')) or 
                   die(var_dump(imap_errors()));

This gives me the current error of too many login failures, but this error changes bases on the settings i try above within imap_open so im not sure this is an actual true error code

Warning: imap_open(): Couldn't open stream {outlook.office365.com:993/imap/ssl/novalidate-cert}Inbox in imap.connect2.php on line 14 array(2) { [0]=> string(13) "LOGIN failed." [1]=> string(23) "Too many login failures" }

So if i change the code to something more simple without the DISABLE_AUTHENTICATOR additional, i still get an error. See code and error below

$mbox=imap_open($host,$username,$password) or 
                   die(var_dump(imap_errors()));

I get authenticate failed

Warning: imap_open(): Couldn't open stream {outlook.office365.com:993/imap/ssl/novalidate-cert}Inbox in /home/tnslive/domains/manage.tnslive.com/public_html/webmail/imap.connect2.php on line 13
array(3) { [0]=> string(56) "Retrying PLAIN authentication after AUTHENTICATE failed." [1]=> string(56) "Retrying PLAIN authentication after AUTHENTICATE failed." [2]=> string(57) "Can not authenticate to IMAP server: AUTHENTICATE failed." }

I've been trying this for a week and tried other slight alternatives with the domain used in the imap_open function for office365 .... im at a massive loss here and i've bought office365 for a lot of people and now we cant use our application with the primary email via our php software. Nightmare.

Upvotes: 2

Views: 8711

Answers (1)

Alexey Burdin
Alexey Burdin

Reputation: 132

This code works since 2018 still (on a production server)

$host = $mailboxes[$mailbox]['host'];
$enc = '/imap/ssl/novalidate-cert';
$this->mailbox = "{" . $host . $enc . "}";
imap_open($this->mailbox, $config['username'], $config['password'], 1, 0, array('DISABLE_AUTHENTICATOR' => 'GSSAPI'));

Although I should mention 2 more things.

  1. outlook.office365.com sometimes resolves to IPv6, but php doesn't work well with IPv6, so you will need to resolve outlook.office365.com IPv4 manually somehow (personally I have a pre-defined array with IPs and get a random index from the array each time).
  2. You should enable AUTHENTICATE PLAIN method (see here) <-- I think it's the thing you want.

Upvotes: 0

Related Questions