'swiftmailer Swift_SmtpTransport compatibility issue with office 365 OAUTH

Getting error when sending mail using the code:

$transport = (new Swift_SmtpTransport('smtp.office365.com', 587, 'tls'))
    ->setAuthMode('XOAUTH2')
   ->setUsername('[email protected]')
    ->setPassword($accessToken)

This works well with Gmail OAuth. When I try with office 365 accesstoken, I am getting error like:

Failed to authenticate on SMTP server with username "[email protected]" using 1 possible authenticators. Authenticator XOAUTH2 returned Expected response code 235 but got code "535", with message "535 5.7.3 Authentication unsuccessful [TYCPR01CA0119.jpnprd01.prod.outlook.com] " Log data: ++ Starting Swift_SmtpTransport << 220 TYCPR01CA0119.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 20 May 2022 04:33:17 +0000 >> EHLO maruthi.ichigaya.biz << 250-TYCPR01CA0119.outlook.office365.com Hello [18.181.120.148] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 >> STARTTLS << 220 2.0.0 SMTP server ready >> EHLO maruthi.ichigaya.biz << 250-TYCPR01CA0119.outlook.office365.com Hello [18.181.120.148] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-AUTH LOGIN XOAUTH2 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 >> AUTH XOAUTH2



Solution 1:[1]

In this Scenario the error may be two ways whether its The PHP app wasn’t able to connect to the SMTP server, or The app was able to connect but the login failed.

And other possible way of getting error,If your mail server password contains a # then you should quote the environment string since everything after # will be taken as comment.

you can also refer this link for this error and can be fixed and also use this link are very useful.

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 VenkatesanSettu-MT