class.phpmaileroauthgoogle.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * PHPMailer - PHP email creation and transport class.
  4. * PHP Version 5.4
  5. * @package PHPMailer
  6. * @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
  7. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  8. * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  9. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  10. * @author Brent R. Matzelle (original founder)
  11. * @copyright 2012 - 2014 Marcus Bointon
  12. * @copyright 2010 - 2012 Jim Jagielski
  13. * @copyright 2004 - 2009 Andy Prevost
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  15. * @note This program is distributed in the hope that it will be useful - WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE.
  18. */
  19. /**
  20. * PHPMailerOAuthGoogle - Wrapper for League OAuth2 Google provider.
  21. * @package PHPMailer
  22. * @author @sherryl4george
  23. * @author Marcus Bointon (@Synchro) <phpmailer@synchromedia.co.uk>
  24. * @link https://github.com/thephpleague/oauth2-client
  25. */
  26. class PHPMailerOAuthGoogle
  27. {
  28. private $oauthUserEmail = '';
  29. private $oauthRefreshToken = '';
  30. private $oauthClientId = '';
  31. private $oauthClientSecret = '';
  32. /**
  33. * @param string $UserEmail
  34. * @param string $ClientSecret
  35. * @param string $ClientId
  36. * @param string $RefreshToken
  37. */
  38. public function __construct(
  39. $UserEmail,
  40. $ClientSecret,
  41. $ClientId,
  42. $RefreshToken
  43. ) {
  44. $this->oauthClientId = $ClientId;
  45. $this->oauthClientSecret = $ClientSecret;
  46. $this->oauthRefreshToken = $RefreshToken;
  47. $this->oauthUserEmail = $UserEmail;
  48. }
  49. private function getProvider()
  50. {
  51. return new League\OAuth2\Client\Provider\Google([
  52. 'clientId' => $this->oauthClientId,
  53. 'clientSecret' => $this->oauthClientSecret
  54. ]);
  55. }
  56. private function getGrant()
  57. {
  58. return new \League\OAuth2\Client\Grant\RefreshToken();
  59. }
  60. private function getToken()
  61. {
  62. $provider = $this->getProvider();
  63. $grant = $this->getGrant();
  64. return $provider->getAccessToken($grant, ['refresh_token' => $this->oauthRefreshToken]);
  65. }
  66. public function getOauth64()
  67. {
  68. $token = $this->getToken();
  69. return base64_encode("user=" . $this->oauthUserEmail . "\001auth=Bearer " . $token . "\001\001");
  70. }
  71. }