SmtpTransport.php 12 KB

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