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

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