'Getting Failed to authenticate error on smtp when trying to send mail using swiftmailer with office 365 oauth2 accesstoken
I am trying to send mails using swiftmailer and office 365 which failed to authenticate. I have purchased an office 365 account, azure subscription. My environment is php. Steps I did:
- Created office 365 account
- Created an app in Azure AD
- created client secret
- Set Authentication as "Accounts in any organizational directory (Any Azure AD directory - Multitenant)"
- set API Permissions
- Downloaded Microsoft Graph sdk to generate accessToken
- I generated accesstoken and used the it on swiftmailer transport as password and setting the auth type as "XOAUTH2"
Here is the list of screenshots supporting above activity.
I used below sample code from "https://github.com/microsoftgraph/msgraph-sdk-php" to get accesstoken.
$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'scope' => 'https://graph.microsoft.com/.default',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$accessToken = $token->access_token;
Below is the complete code
require_once 'swift/vendor/autoload.php';
require_once 'graph/vendor/autoload.php';
$tenant_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//$graphMailer = new graphMailer($tenant_id, $client_id, $secret_id);
$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/' . $tenant_id . '/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $client_id,
'client_secret' => $clientSecret,
'scope' => 'https://graph.microsoft.com/.default',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$accessToken = $token->access_token;
// echo $accessToken;
$transport = (new Swift_SmtpTransport('smtp.office365.com', 587, 'tls'))
->setAuthMode('XOAUTH2')
->setUsername('[email protected]')
->setPassword($accessToken)
;
$transport->start();
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['[email protected]' => 'Anish V M'])
->setTo(['[email protected]', '[email protected]' => 'Anish'])
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
echo $result;
I am wondering if any one can point me in right direction. Or tell me if I miss something.
Thanks
Solution 1:[1]
Disabling one setting on my WHM root login solved the failed to authenticate error on smtp for me:
WHM > Home > Server Configuration > Tweak Settings >
Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak) [?]
Solution 2:[2]
When you create an Envelope, in the API defined a section for the request, which allows you to define the desired format of the data: connecteventdata
Documentation of this object also links an article, which explains it in more details.
So, you have more options to specify the includeData among with the "documents" example:
The eventNotification object now includes a new attribute, eventData. The attribute is set to an object with three attributes: {version, format, and includeData}
includeData is an array of strings with valid items: "custom_fields", "extensions", "folders", "recipients", "powerform", "tabs", "payment_tabs", "documents", "attachments" - see description by the first link, 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SuryasriKamini-MT |
| Solution 2 | Dmitry Kravtsov |
