Forum Discussion

Sani-Asnain's avatar
Sani-Asnain
Copper Contributor
Apr 19, 2023

Bad User is Authenticated But Not Connected

Hi Everybody,

 

We are using PHP-IMAP to connect to Microsoft 365 outlook mail box using oAuth2.0.

Here is out sample code.

                                $cm = new ClientManager($options = ["options" => ["debug" => true]]);
$client = $cm->make([
'host' => 'outlook.office365.com',
'port' => 993,
'encryption' => 'ssl',
'validate_cert' => true,
'username' => 'user@********.com',
'password' => $access_token,
'protocol' => 'imap',
'authentication' => "oauth"
]);

try {
$client->connect();
$status = $client->isConnected(); //$status = 1
$folders = $client->getFolders($hierarchical = true);
print_r($folders);die();
}catch (Exception $e) {
echo 'Exception : ', $e->getMessage(), "\n";
}

When we call $client->isConnected() it returns 1, but when the debugger is enabled we get the response as BAD User is authenticated but not connected.

 

We have tried each and everything available on the web related to this issue.
- IMAP is enabled at 365 Admin Panel

- MFA is disabled as suggested by many
- API Permissions added at Azure AAD Application

- Service Principle added via power shell and given access to mailbox

 

Any help in this regard will be highly appreciated

 

Resources