Update: We are trying to improve the setup in Symfony to make most of this gist hopefully not needed anymore (except the token provider): symfony/symfony#52585
I banged my head against this for a while, but finally got it to work.
What you need to set this up:
- the user name (= email address) of your email account
- tenant id for your email account (a uuid)
- client id for your email account (a uuid)
- a secret token for oauth (for me that was 40 characters long)
I then set up the following services (let me know if there is a more elegant way of setting this up with symfony mailer - i did not see how else i can dynamically do the oauth2 login to get a fresh token)
services:
App\Infrastructure\Email\Office365OAuthTokenProvider:
$tenant: '%env(resolve:EMAIL_TENANT)%'
$clientId: '%env(resolve:EMAIL_CLIENT_ID)%'
$clientSecret: '%env(resolve:EMAIL_CLIENT_SECRET)%'
App\Infrastructure\Email\OAuthEsmtpTransportFactoryDecorator:
decorates: mailer.transport_factory.smtp
arguments:
$inner: '@.inner'
$authenticator: '@App\Infrastructure\Email\XOAuth2Authenticator'
and in .env set up the variables:
### symfony/mailer ###
# Username is the full email address. Need to urlencode the "@" in the username.
MAILER_DSN=smtp://email%40domain.com:@smtp.office365.com:587
###< symfony/mailer ###
EMAIL_TENANT=cafebabe-cafe-babe-cafe-babecafebabe
EMAIL_CLIENT_ID=cafebabe-cafe-babe-cafe-babecafebabe
EMAIL_CLIENT_SECRET=
And at runtime inject the right secret token.
Ran into the same issue as described above, followed these steps in this article and managed to make it work :)
https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth