3 * PHPMailer - PHP email creation and transport class.
6 * @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
8 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
9 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
10 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
11 * @author Brent R. Matzelle (original founder)
12 * @copyright 2012 - 2015 Marcus Bointon
13 * @copyright 2010 - 2012 Jim Jagielski
14 * @copyright 2004 - 2009 Andy Prevost
15 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
16 * @note This program is distributed in the hope that it will be useful - WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE.
21 namespace PHPMailer\PHPMailer
;
23 use League\OAuth2\Client\Grant\RefreshToken
;
24 use League\OAuth2\Client\Provider\AbstractProvider
;
25 use League\OAuth2\Client\Token\AccessToken
;
28 * OAuth - OAuth2 authentication wrapper class.
29 * Uses the oauth2-client package from the League of Extraordinary Packages.
31 * @see http://oauth2-client.thephpleague.com
33 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
38 * An instance of the League OAuth Client Provider.
40 * @var AbstractProvider
45 * The current OAuth access token.
49 protected $oauthToken;
52 * The user's email address, usually used as the login ID
53 * and also the from address when sending email.
57 protected $oauthUserEmail = '';
60 * The client secret, generated in the app definition of the service you're connecting to.
64 protected $oauthClientSecret = '';
67 * The client ID, generated in the app definition of the service you're connecting to.
71 protected $oauthClientId = '';
74 * The refresh token, used to obtain new AccessTokens.
78 protected $oauthRefreshToken = '';
83 * @param array $options Associative array containing
84 * `provider`, `userName`, `clientSecret`, `clientId` and `refreshToken` elements
86 public function __construct($options)
88 $this->provider
= $options['provider'];
89 $this->oauthUserEmail
= $options['userName'];
90 $this->oauthClientSecret
= $options['clientSecret'];
91 $this->oauthClientId
= $options['clientId'];
92 $this->oauthRefreshToken
= $options['refreshToken'];
96 * Get a new RefreshToken.
98 * @return RefreshToken
100 protected function getGrant()
102 return new RefreshToken();
106 * Get a new AccessToken.
108 * @return AccessToken
110 protected function getToken()
112 return $this->provider
->getAccessToken(
114 ['refresh_token' => $this->oauthRefreshToken
]
119 * Generate a base64-encoded OAuth token.
123 public function getOauth64()
125 // Get a new token if it's not available or has expired
126 if (null === $this->oauthToken
or $this->oauthToken
->hasExpired()) {
127 $this->oauthToken
= $this->getToken();
130 return base64_encode(
132 $this->oauthUserEmail
.