SmtpTransport.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 = 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. public function setRestartThreshold(int $threshold, int $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($domain, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
  95. $domain = '['.$domain.']';
  96. } elseif (filter_var($domain, \FILTER_VALIDATE_IP, \FILTER_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 $message, Envelope $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 '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. try {
  176. foreach (AbstractStream::replace("\r\n.", "\r\n..", $message->toIterable()) as $chunk) {
  177. $this->stream->write($chunk, false);
  178. }
  179. $this->stream->flush();
  180. } catch (TransportExceptionInterface $e) {
  181. throw $e;
  182. } catch (\Exception $e) {
  183. $this->stream->terminate();
  184. $this->started = false;
  185. $this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__));
  186. throw $e;
  187. }
  188. $this->executeCommand("\r\n.\r\n", [250]);
  189. $message->appendDebug($this->stream->getDebug());
  190. $this->lastMessageTime = microtime(true);
  191. } catch (TransportExceptionInterface $e) {
  192. $e->appendDebug($this->stream->getDebug());
  193. $this->lastMessageTime = 0;
  194. throw $e;
  195. }
  196. }
  197. protected function doHeloCommand(): void
  198. {
  199. $this->executeCommand(sprintf("HELO %s\r\n", $this->domain), [250]);
  200. }
  201. private function doMailFromCommand(string $address): void
  202. {
  203. $this->executeCommand(sprintf("MAIL FROM:<%s>\r\n", $address), [250]);
  204. }
  205. private function doRcptToCommand(string $address): void
  206. {
  207. $this->executeCommand(sprintf("RCPT TO:<%s>\r\n", $address), [250, 251, 252]);
  208. }
  209. private function start(): void
  210. {
  211. if ($this->started) {
  212. return;
  213. }
  214. $this->getLogger()->debug(sprintf('Email transport "%s" starting', __CLASS__));
  215. $this->stream->initialize();
  216. $this->assertResponseCode($this->getFullResponse(), [220]);
  217. $this->doHeloCommand();
  218. $this->started = true;
  219. $this->lastMessageTime = 0;
  220. $this->getLogger()->debug(sprintf('Email transport "%s" started', __CLASS__));
  221. }
  222. private function stop(): void
  223. {
  224. if (!$this->started) {
  225. return;
  226. }
  227. $this->getLogger()->debug(sprintf('Email transport "%s" stopping', __CLASS__));
  228. try {
  229. $this->executeCommand("QUIT\r\n", [221]);
  230. } catch (TransportExceptionInterface $e) {
  231. } finally {
  232. $this->stream->terminate();
  233. $this->started = false;
  234. $this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__));
  235. }
  236. }
  237. private function ping(): void
  238. {
  239. if (!$this->started) {
  240. return;
  241. }
  242. try {
  243. $this->executeCommand("NOOP\r\n", [250]);
  244. } catch (TransportExceptionInterface $e) {
  245. $this->stop();
  246. }
  247. }
  248. /**
  249. * @throws TransportException if a response code is incorrect
  250. */
  251. private function assertResponseCode(string $response, array $codes): void
  252. {
  253. if (!$codes) {
  254. throw new LogicException('You must set the expected response code.');
  255. }
  256. if (!$response) {
  257. throw new TransportException(sprintf('Expected response code "%s" but got an empty response.', implode('/', $codes)));
  258. }
  259. [$code] = sscanf($response, '%3d');
  260. $valid = \in_array($code, $codes);
  261. if (!$valid) {
  262. throw new TransportException(sprintf('Expected response code "%s" but got code "%s", with message "%s".', implode('/', $codes), $code, trim($response)), $code);
  263. }
  264. }
  265. private function getFullResponse(): string
  266. {
  267. $response = '';
  268. do {
  269. $line = $this->stream->readLine();
  270. $response .= $line;
  271. } while ($line && isset($line[3]) && ' ' !== $line[3]);
  272. return $response;
  273. }
  274. private function checkRestartThreshold(): void
  275. {
  276. // when using sendmail via non-interactive mode, the transport is never "started"
  277. if (!$this->started) {
  278. return;
  279. }
  280. ++$this->restartCounter;
  281. if ($this->restartCounter < $this->restartThreshold) {
  282. return;
  283. }
  284. $this->stop();
  285. if (0 < $sleep = $this->restartThresholdSleep) {
  286. $this->getLogger()->debug(sprintf('Email transport "%s" sleeps for %d seconds after stopping', __CLASS__, $sleep));
  287. sleep($sleep);
  288. }
  289. $this->start();
  290. $this->restartCounter = 0;
  291. }
  292. public function __sleep()
  293. {
  294. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  295. }
  296. public function __wakeup()
  297. {
  298. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  299. }
  300. public function __destruct()
  301. {
  302. $this->stop();
  303. }
  304. }