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

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