vendor/symfony/mailer/Transport/Smtp/EsmtpTransport.php line 123

Open in your IDE?
  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;
  11. use Psr\EventDispatcher\EventDispatcherInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Mailer\Exception\TransportException;
  14. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  15. use Symfony\Component\Mailer\Transport\Smtp\Auth\AuthenticatorInterface;
  16. use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
  17. /**
  18.  * Sends Emails over SMTP with ESMTP support.
  19.  *
  20.  * @author Fabien Potencier <fabien@symfony.com>
  21.  * @author Chris Corbyn
  22.  */
  23. class EsmtpTransport extends SmtpTransport
  24. {
  25.     private $authenticators = [];
  26.     private $username '';
  27.     private $password '';
  28.     public function __construct(string $host 'localhost'int $port 0, ?bool $tls null, ?EventDispatcherInterface $dispatcher null, ?LoggerInterface $logger null)
  29.     {
  30.         parent::__construct(null$dispatcher$logger);
  31.         // order is important here (roughly most secure and popular first)
  32.         $this->authenticators = [
  33.             new Auth\CramMd5Authenticator(),
  34.             new Auth\LoginAuthenticator(),
  35.             new Auth\PlainAuthenticator(),
  36.             new Auth\XOAuth2Authenticator(),
  37.         ];
  38.         /** @var SocketStream $stream */
  39.         $stream $this->getStream();
  40.         if (null === $tls) {
  41.             if (465 === $port) {
  42.                 $tls true;
  43.             } else {
  44.                 $tls = \defined('OPENSSL_VERSION_NUMBER') && === $port && 'localhost' !== $host;
  45.             }
  46.         }
  47.         if (!$tls) {
  48.             $stream->disableTls();
  49.         }
  50.         if (=== $port) {
  51.             $port $tls 465 25;
  52.         }
  53.         $stream->setHost($host);
  54.         $stream->setPort($port);
  55.     }
  56.     /**
  57.      * @return $this
  58.      */
  59.     public function setUsername(string $username): self
  60.     {
  61.         $this->username $username;
  62.         return $this;
  63.     }
  64.     public function getUsername(): string
  65.     {
  66.         return $this->username;
  67.     }
  68.     /**
  69.      * @return $this
  70.      */
  71.     public function setPassword(string $password): self
  72.     {
  73.         $this->password $password;
  74.         return $this;
  75.     }
  76.     public function getPassword(): string
  77.     {
  78.         return $this->password;
  79.     }
  80.     public function addAuthenticator(AuthenticatorInterface $authenticator): void
  81.     {
  82.         $this->authenticators[] = $authenticator;
  83.     }
  84.     protected function doHeloCommand(): void
  85.     {
  86.         if (!$capabilities $this->callHeloCommand()) {
  87.             return;
  88.         }
  89.         /** @var SocketStream $stream */
  90.         $stream $this->getStream();
  91.         // WARNING: !$stream->isTLS() is right, 100% sure :)
  92.         // if you think that the ! should be removed, read the code again
  93.         // if doing so "fixes" your issue then it probably means your SMTP server behaves incorrectly or is wrongly configured
  94.         if (!$stream->isTLS() && \defined('OPENSSL_VERSION_NUMBER') && \array_key_exists('STARTTLS'$capabilities)) {
  95.             $this->executeCommand("STARTTLS\r\n", [220]);
  96.             if (!$stream->startTLS()) {
  97.                 throw new TransportException('Unable to connect with STARTTLS.');
  98.             }
  99.             $capabilities $this->callHeloCommand();
  100.         }
  101.         if (\array_key_exists('AUTH'$capabilities)) {
  102.             $this->handleAuth($capabilities['AUTH']);
  103.         }
  104.     }
  105.     private function callHeloCommand(): array
  106.     {
  107.         try {
  108.             $response $this->executeCommand(sprintf("EHLO %s\r\n"$this->getLocalDomain()), [250]);
  109.         } catch (TransportExceptionInterface $e) {
  110.             try {
  111.                 parent::doHeloCommand();
  112.                 return [];
  113.             } catch (TransportExceptionInterface $ex) {
  114.                 if (!$ex->getCode()) {
  115.                     throw $e;
  116.                 }
  117.                 throw $ex;
  118.             }
  119.         }
  120.         $capabilities = [];
  121.         $lines explode("\r\n"trim($response));
  122.         array_shift($lines);
  123.         foreach ($lines as $line) {
  124.             if (preg_match('/^[0-9]{3}[ -]([A-Z0-9-]+)((?:[ =].*)?)$/Di'$line$matches)) {
  125.                 $value strtoupper(ltrim($matches[2], ' ='));
  126.                 $capabilities[strtoupper($matches[1])] = $value explode(' '$value) : [];
  127.             }
  128.         }
  129.         return $capabilities;
  130.     }
  131.     private function handleAuth(array $modes): void
  132.     {
  133.         if (!$this->username) {
  134.             return;
  135.         }
  136.         $authNames = [];
  137.         $errors = [];
  138.         $modes array_map('strtolower'$modes);
  139.         foreach ($this->authenticators as $authenticator) {
  140.             if (!\in_array(strtolower($authenticator->getAuthKeyword()), $modestrue)) {
  141.                 continue;
  142.             }
  143.             $authNames[] = $authenticator->getAuthKeyword();
  144.             try {
  145.                 $authenticator->authenticate($this);
  146.                 return;
  147.             } catch (TransportExceptionInterface $e) {
  148.                 try {
  149.                     $this->executeCommand("RSET\r\n", [250]);
  150.                 } catch (TransportExceptionInterface $_) {
  151.                     // ignore this exception as it probably means that the server error was final
  152.                 }
  153.                 // keep the error message, but tries the other authenticators
  154.                 $errors[$authenticator->getAuthKeyword()] = $e->getMessage();
  155.             }
  156.         }
  157.         if (!$authNames) {
  158.             throw new TransportException(sprintf('Failed to find an authenticator supported by the SMTP server, which currently supports: "%s".'implode('", "'$modes)));
  159.         }
  160.         $message sprintf('Failed to authenticate on SMTP server with username "%s" using the following authenticators: "%s".'$this->usernameimplode('", "'$authNames));
  161.         foreach ($errors as $name => $error) {
  162.             $message .= sprintf(' Authenticator "%s" returned "%s".'$name$error);
  163.         }
  164.         throw new TransportException($message);
  165.     }
  166. }