vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php line 43

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\Log\LoggerInterface;
  12. use Symfony\Component\Mailer\Envelope;
  13. use Symfony\Component\Mailer\Exception\LogicException;
  14. use Symfony\Component\Mailer\Exception\TransportException;
  15. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  16. use Symfony\Component\Mailer\SentMessage;
  17. use Symfony\Component\Mailer\Transport\AbstractTransport;
  18. use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
  19. use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
  20. use Symfony\Component\Mime\RawMessage;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22. /**
  23.  * Sends emails over SMTP.
  24.  *
  25.  * @author Fabien Potencier <fabien@symfony.com>
  26.  * @author Chris Corbyn
  27.  */
  28. class SmtpTransport extends AbstractTransport
  29. {
  30.     private $started false;
  31.     private $restartThreshold 100;
  32.     private $restartThresholdSleep 0;
  33.     private $restartCounter;
  34.     private $pingThreshold 100;
  35.     private $lastMessageTime 0;
  36.     private $stream;
  37.     private $domain '[127.0.0.1]';
  38.     public function __construct(AbstractStream $stream nullEventDispatcherInterface $dispatcher nullLoggerInterface $logger null)
  39.     {
  40.         parent::__construct($dispatcher$logger);
  41.         $this->stream $stream ?: new SocketStream();
  42.     }
  43.     public function getStream(): AbstractStream
  44.     {
  45.         return $this->stream;
  46.     }
  47.     /**
  48.      * Sets the maximum number of messages to send before re-starting the transport.
  49.      *
  50.      * By default, the threshold is set to 100 (and no sleep at restart).
  51.      *
  52.      * @param int $threshold The maximum number of messages (0 to disable)
  53.      * @param int $sleep     The number of seconds to sleep between stopping and re-starting the transport
  54.      */
  55.     public function setRestartThreshold(int $thresholdint $sleep 0): self
  56.     {
  57.         $this->restartThreshold $threshold;
  58.         $this->restartThresholdSleep $sleep;
  59.         return $this;
  60.     }
  61.     /**
  62.      * Sets the minimum number of seconds required between two messages, before the server is pinged.
  63.      * If the transport wants to send a message and the time since the last message exceeds the specified threshold,
  64.      * the transport will ping the server first (NOOP command) to check if the connection is still alive.
  65.      * Otherwise the message will be sent without pinging the server first.
  66.      *
  67.      * Do not set the threshold too low, as the SMTP server may drop the connection if there are too many
  68.      * non-mail commands (like pinging the server with NOOP).
  69.      *
  70.      * By default, the threshold is set to 100 seconds.
  71.      *
  72.      * @param int $seconds The minimum number of seconds between two messages required to ping the server
  73.      *
  74.      * @return $this
  75.      */
  76.     public function setPingThreshold(int $seconds): self
  77.     {
  78.         $this->pingThreshold $seconds;
  79.         return $this;
  80.     }
  81.     /**
  82.      * Sets the name of the local domain that will be used in HELO.
  83.      *
  84.      * This should be a fully-qualified domain name and should be truly the domain
  85.      * you're using.
  86.      *
  87.      * If your server does not have a domain name, use the IP address. This will
  88.      * automatically be wrapped in square brackets as described in RFC 5321,
  89.      * section 4.1.3.
  90.      */
  91.     public function setLocalDomain(string $domain): self
  92.     {
  93.         if ('' !== $domain && '[' !== $domain[0]) {
  94.             if (filter_var($domainFILTER_VALIDATE_IPFILTER_FLAG_IPV4)) {
  95.                 $domain '['.$domain.']';
  96.             } elseif (filter_var($domainFILTER_VALIDATE_IPFILTER_FLAG_IPV6)) {
  97.                 $domain '[IPv6:'.$domain.']';
  98.             }
  99.         }
  100.         $this->domain $domain;
  101.         return $this;
  102.     }
  103.     /**
  104.      * Gets the name of the domain that will be used in HELO.
  105.      *
  106.      * If an IP address was specified, this will be returned wrapped in square
  107.      * brackets as described in RFC 5321, section 4.1.3.
  108.      */
  109.     public function getLocalDomain(): string
  110.     {
  111.         return $this->domain;
  112.     }
  113.     public function send(RawMessage $messageEnvelope $envelope null): ?SentMessage
  114.     {
  115.         try {
  116.             $message parent::send($message$envelope);
  117.         } catch (TransportExceptionInterface $e) {
  118.             if ($this->started) {
  119.                 try {
  120.                     $this->executeCommand("RSET\r\n", [250]);
  121.                 } catch (TransportExceptionInterface $_) {
  122.                     // ignore this exception as it probably means that the server error was final
  123.                 }
  124.             }
  125.             throw $e;
  126.         }
  127.         $this->checkRestartThreshold();
  128.         return $message;
  129.     }
  130.     public function __toString(): string
  131.     {
  132.         if ($this->stream instanceof SocketStream) {
  133.             $name sprintf('smtp%s://%s', ($tls $this->stream->isTLS()) ? 's' ''$this->stream->getHost());
  134.             $port $this->stream->getPort();
  135.             if (!(25 === $port || ($tls && 465 === $port))) {
  136.                 $name .= ':'.$port;
  137.             }
  138.             return $name;
  139.         }
  140.         return sprintf('smtp://sendmail');
  141.     }
  142.     /**
  143.      * Runs a command against the stream, expecting the given response codes.
  144.      *
  145.      * @param int[] $codes
  146.      *
  147.      * @return string The server response
  148.      *
  149.      * @throws TransportException when an invalid response if received
  150.      *
  151.      * @internal
  152.      */
  153.     public function executeCommand(string $command, array $codes): string
  154.     {
  155.         $this->stream->write($command);
  156.         $response $this->getFullResponse();
  157.         $this->assertResponseCode($response$codes);
  158.         return $response;
  159.     }
  160.     protected function doSend(SentMessage $message): void
  161.     {
  162.         if (microtime(true) - $this->lastMessageTime $this->pingThreshold) {
  163.             $this->ping();
  164.         }
  165.         if (!$this->started) {
  166.             $this->start();
  167.         }
  168.         try {
  169.             $envelope $message->getEnvelope();
  170.             $this->doMailFromCommand($envelope->getSender()->getAddress());
  171.             foreach ($envelope->getRecipients() as $recipient) {
  172.                 $this->doRcptToCommand($recipient->getAddress());
  173.             }
  174.             $this->executeCommand("DATA\r\n", [354]);
  175.             foreach (AbstractStream::replace("\r\n.""\r\n.."$message->toIterable()) as $chunk) {
  176.                 $this->stream->write($chunkfalse);
  177.             }
  178.             $this->stream->flush();
  179.             $this->executeCommand("\r\n.\r\n", [250]);
  180.             $message->appendDebug($this->stream->getDebug());
  181.             $this->lastMessageTime microtime(true);
  182.         } catch (TransportExceptionInterface $e) {
  183.             $e->appendDebug($this->stream->getDebug());
  184.             $this->lastMessageTime 0;
  185.             throw $e;
  186.         }
  187.     }
  188.     protected function doHeloCommand(): void
  189.     {
  190.         $this->executeCommand(sprintf("HELO %s\r\n"$this->domain), [250]);
  191.     }
  192.     private function doMailFromCommand(string $address): void
  193.     {
  194.         $this->executeCommand(sprintf("MAIL FROM:<%s>\r\n"$address), [250]);
  195.     }
  196.     private function doRcptToCommand(string $address): void
  197.     {
  198.         $this->executeCommand(sprintf("RCPT TO:<%s>\r\n"$address), [250251252]);
  199.     }
  200.     private function start(): void
  201.     {
  202.         if ($this->started) {
  203.             return;
  204.         }
  205.         $this->getLogger()->debug(sprintf('Email transport "%s" starting'__CLASS__));
  206.         $this->stream->initialize();
  207.         $this->assertResponseCode($this->getFullResponse(), [220]);
  208.         $this->doHeloCommand();
  209.         $this->started true;
  210.         $this->lastMessageTime 0;
  211.         $this->getLogger()->debug(sprintf('Email transport "%s" started'__CLASS__));
  212.     }
  213.     private function stop(): void
  214.     {
  215.         if (!$this->started) {
  216.             return;
  217.         }
  218.         $this->getLogger()->debug(sprintf('Email transport "%s" stopping'__CLASS__));
  219.         try {
  220.             $this->executeCommand("QUIT\r\n", [221]);
  221.         } catch (TransportExceptionInterface $e) {
  222.         } finally {
  223.             $this->stream->terminate();
  224.             $this->started false;
  225.             $this->getLogger()->debug(sprintf('Email transport "%s" stopped'__CLASS__));
  226.         }
  227.     }
  228.     private function ping(): void
  229.     {
  230.         if (!$this->started) {
  231.             return;
  232.         }
  233.         try {
  234.             $this->executeCommand("NOOP\r\n", [250]);
  235.         } catch (TransportExceptionInterface $e) {
  236.             $this->stop();
  237.         }
  238.     }
  239.     /**
  240.      * @throws TransportException if a response code is incorrect
  241.      */
  242.     private function assertResponseCode(string $response, array $codes): void
  243.     {
  244.         if (!$codes) {
  245.             throw new LogicException('You must set the expected response code.');
  246.         }
  247.         if (!$response) {
  248.             throw new TransportException(sprintf('Expected response code "%s" but got an empty response.'implode('/'$codes)));
  249.         }
  250.         list($code) = sscanf($response'%3d');
  251.         $valid = \in_array($code$codes);
  252.         if (!$valid) {
  253.             throw new TransportException(sprintf('Expected response code "%s" but got code "%s", with message "%s".'implode('/'$codes), $codetrim($response)), $code);
  254.         }
  255.     }
  256.     private function getFullResponse(): string
  257.     {
  258.         $response '';
  259.         do {
  260.             $line $this->stream->readLine();
  261.             $response .= $line;
  262.         } while ($line && isset($line[3]) && ' ' !== $line[3]);
  263.         return $response;
  264.     }
  265.     private function checkRestartThreshold(): void
  266.     {
  267.         // when using sendmail via non-interactive mode, the transport is never "started"
  268.         if (!$this->started) {
  269.             return;
  270.         }
  271.         ++$this->restartCounter;
  272.         if ($this->restartCounter $this->restartThreshold) {
  273.             return;
  274.         }
  275.         $this->stop();
  276.         if ($sleep $this->restartThresholdSleep) {
  277.             $this->getLogger()->debug(sprintf('Email transport "%s" sleeps for %d seconds after stopping'__CLASS__$sleep));
  278.             sleep($sleep);
  279.         }
  280.         $this->start();
  281.         $this->restartCounter 0;
  282.     }
  283.     public function __destruct()
  284.     {
  285.         $this->stop();
  286.     }
  287. }