0

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

fcaserio
  • 726
  • 1
  • 9
  • 18
  • With OAuth you have to exchange the users password (via a web browser for the most common usage) for an access token and a refresh token. You never use the password. You supply the access token as the Bearer. It only lasts for an hour (usually) and then you have to use the Refresh token to get another one. You will also need an OAuth library to do this exchanging for you. – Max Jan 05 '23 at 22:22
  • That is preciselly what I'm asking, what is the best PHP library to connect with imap to an outlook email account? – fcaserio Jan 05 '23 at 22:54
  • Yes, you're pointing out IMAP libraries you've tried; you need an OAuth library _in addition_ to an IMAP library. They are two separate things. However suggesting libraries is off-topic for StackOverflow. There appears to be a list here: https://oauth.net/code/php/ – Max Jan 06 '23 at 03:20
  • Here's a related question where they do the Oauth steps: https://stackoverflow.com/questions/73370661/php-connect-mailbox-office-365-with-oauth – Max Jan 06 '23 at 03:22
  • 1
    Thank you, the post above looks promising – fcaserio Jan 06 '23 at 13:55

0 Answers0