What is the best php option to connect to outlook imap with oAuth?

613 Views Asked by At

What would be the best option to connect to an outlook email account with PHP? I´m not sure how to declare my credentials with oAuth, should I use my e-mail as username or the client Id key as username? Should I use client secret as password?

I've been trying the following solutions with no success:

https://github.com/javanile/php-imap2

include "inc/php-imap2/vendor/autoload.php";

// Start connection
$mailbox = "{smtp.office365.com:993/imap/ssl/novalidate-cert}";
$token = base64_encode("user=" . $email . "\1auth=Bearer " . $passw . "\1\1");
$imap = @imap2_open($mailbox, $email, $token, OP_XOAUTH2);

// List all mailboxes
if ($imap) {
    $mailboxes = imap2_getmailboxes($imap, $mailbox, '*');    
    var_dump($mailboxes);
} else {
    $error = imap2_last_error();
    echo $error;
}

Return

Can not authenticate to IMAP server: A0001 NO AUTHENTICATE failed.

https://github.com/Webklex/php-imap

include "inc/php-imap/vendor/autoload.php";

use Webklex\PHPIMAP\ClientManager;
use Webklex\PHPIMAP\Client;

$cm = new ClientManager($options = []);

$client = $cm->make([
    'host'          => "smtp.office365.com",
    'port'          => 993,
    'encryption'    => 'ssl',
    'validate_cert' => true,
    'username'      => $email,
    'password'      => $passw,
    'protocol'      => 'imap',
    'authentication' => 'oauth',
]);

//Connect to the IMAP Server
$client->connect();

Return

got failure response: NO AUTHENTICATE failed.

PHP Fatal error:  Uncaught Webklex\PHPIMAP\Exceptions\AuthFailedException in C:\inetpub\wwwroot\inc\php-imap\src\Client.php:385
Stack trace:
#0 C:\inetpub\wwwroot\inc\php-imap\src\Client.php(371): Webklex\PHPIMAP\Client->authenticate()
#1 C:\inetpub\wwwroot\test.php(57): Webklex\PHPIMAP\Client->connect()
#2 {main}

Next Webklex\PHPIMAP\Exceptions\ConnectionFailedException: connection setup failed in C:\inetpub\wwwroot\inc\php-imap\src\Client.php:391
Stack trace:
#0 C:\inetpub\wwwroot\inc\php-imap\src\Client.php(371): Webklex\PHPIMAP\Client->authenticate()
#1 C:\inetpub\wwwroot\test.php(57): Webklex\PHPIMAP\Client->connect()
#2 {main}
  thrown in C:\inetpub\wwwroot\inc\php-imap\src\Client.php on line 391

Any help is appreciated

0

There are 0 best solutions below