XOAuth2Authenticator.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mailer\Transport\Smtp\Auth;
  11. use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
  12. /**
  13. * Handles XOAUTH2 authentication.
  14. *
  15. * @author xu.li<AthenaLightenedMyPath@gmail.com>
  16. *
  17. * @see https://developers.google.com/google-apps/gmail/xoauth2_protocol
  18. */
  19. class XOAuth2Authenticator implements AuthenticatorInterface
  20. {
  21. public function getAuthKeyword(): string
  22. {
  23. return 'XOAUTH2';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. *
  28. * @see https://developers.google.com/google-apps/gmail/xoauth2_protocol#the_sasl_xoauth2_mechanism
  29. */
  30. public function authenticate(EsmtpTransport $client): void
  31. {
  32. $client->executeCommand('AUTH XOAUTH2 '.base64_encode('user='.$client->getUsername()."\1auth=Bearer ".$client->getPassword()."\1\1")."\r\n", [235]);
  33. }
  34. }