3

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?

israel altar
  • 1,768
  • 1
  • 16
  • 24
Aurélien Guillard
  • 1,203
  • 8
  • 20
  • phpmailer is using the refresh token instead – fpilee Sep 04 '17 at 21:32
  • For people who is looking for more info about Gmail SMTP via OAuth2, look into this [link](https://developers.google.com/gmail/imap/xoauth2-protocol). – dikirill Mar 28 '18 at 12:13

1 Answers1

2

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

Community
  • 1
  • 1
Nawfal Serrar
  • 2,213
  • 1
  • 14
  • 22
  • 1
    They give the protocole to be able to use it but they cannot make it for each provider (google, yahoo, etc..) lot of website uses oauth its already easy as it is to get the refresh token they would not bother adding this for each website.. – Nawfal Serrar Jun 09 '15 at 01:43
  • @NawfalSerrar is there a way to hook into Swift_SmtpTransport to determine if authorization has failed so that I can use the refresh token to renew my access token? – bmatovu Jul 27 '18 at 17:50