SwiftMailer gmail oauth refresh access token

3.3k views Asked by At

I would like to send emails with the gmail SMTP using oauth authentification.

On this documentation : https://github.com/swiftmailer/swiftmailer/blob/b11f0054d96fa49200d10401e34d879e46c2a2c6/lib/classes/Swift/Transport/Esmtp/Auth/XOAuth2Authenticator.php

I can find this sample code:

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
  ->setAuthMode('XOAUTH2')
  ->setUsername('YOUR_EMAIL_ADDRESS')
  ->setPassword('YOUR_ACCESS_TOKEN');

Great it's work. But it's work just for 1h because the access token expire after this period. I don't find any way to get a renewal of the access token? How we can passe the refresh token to swiftmailer?

1

There are 1 answers

2
Nawfal Serrar On

setPassword should get the token from where it is updated, so you need to make a function which return this token from wherever it is constantly updated, a web service in the case of google api as follow those links will help :

Google OAuth 2.0 refresh token for web application with public access

https://developers.google.com/accounts/docs/OAuth2WebServer#offline