Forum Discussion
Sani-Asnain
Apr 19, 2023Copper Contributor
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
Please make sure IMAP for the user account as well as security defaults under your tenant
- Sani-AsnainCopper ContributorThank you for your response, Yes IMAP is already enabled and for the rest I have followed the following link
https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth