SendmailTransport.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Mailer\Envelope;
  13. use Symfony\Component\Mailer\SentMessage;
  14. use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
  15. use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
  16. use Symfony\Component\Mailer\Transport\Smtp\Stream\ProcessStream;
  17. use Symfony\Component\Mime\RawMessage;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. /**
  20. * SendmailTransport for sending mail through a Sendmail/Postfix (etc..) binary.
  21. *
  22. * Supported modes are -bs and -t, with any additional flags desired.
  23. * It is advised to use -bs mode since error reporting with -t mode is not
  24. * possible.
  25. *
  26. * Transport can be instanciated through SendmailTransportFactory or NativeTransportFactory:
  27. *
  28. * - SendmailTransportFactory to use most common sendmail path and recommanded options
  29. * - NativeTransportFactory when configuration is set via php.ini
  30. *
  31. * @author Fabien Potencier <fabien@symfony.com>
  32. * @author Chris Corbyn
  33. */
  34. class SendmailTransport extends AbstractTransport
  35. {
  36. private $command = '/usr/sbin/sendmail -bs';
  37. private $stream;
  38. private $transport;
  39. /**
  40. * Constructor.
  41. *
  42. * If using -t mode you are strongly advised to include -oi or -i in the flags.
  43. * For example: /usr/sbin/sendmail -oi -t
  44. * -f<sender> flag will be appended automatically if one is not present.
  45. *
  46. * The recommended mode is "-bs" since it is interactive and failure notifications are hence possible.
  47. */
  48. public function __construct(string $command = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
  49. {
  50. parent::__construct($dispatcher, $logger);
  51. if (null !== $command) {
  52. if (false === strpos($command, ' -bs') && false === strpos($command, ' -t')) {
  53. throw new \InvalidArgumentException(sprintf('Unsupported sendmail command flags "%s"; must be one of "-bs" or "-t" but can include additional flags.', $command));
  54. }
  55. $this->command = $command;
  56. }
  57. $this->stream = new ProcessStream();
  58. if (false !== strpos($this->command, ' -bs')) {
  59. $this->stream->setCommand($this->command);
  60. $this->transport = new SmtpTransport($this->stream, $dispatcher, $logger);
  61. }
  62. }
  63. public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
  64. {
  65. if ($this->transport) {
  66. return $this->transport->send($message, $envelope);
  67. }
  68. return parent::send($message, $envelope);
  69. }
  70. public function __toString(): string
  71. {
  72. if ($this->transport) {
  73. return (string) $this->transport;
  74. }
  75. return 'smtp://sendmail';
  76. }
  77. protected function doSend(SentMessage $message): void
  78. {
  79. $this->getLogger()->debug(sprintf('Email transport "%s" starting', __CLASS__));
  80. $command = $this->command;
  81. if (false === strpos($command, ' -f')) {
  82. $command .= ' -f'.escapeshellarg($message->getEnvelope()->getSender()->getEncodedAddress());
  83. }
  84. $chunks = AbstractStream::replace("\r\n", "\n", $message->toIterable());
  85. if (false === strpos($command, ' -i') && false === strpos($command, ' -oi')) {
  86. $chunks = AbstractStream::replace("\n.", "\n..", $chunks);
  87. }
  88. $this->stream->setCommand($command);
  89. $this->stream->initialize();
  90. foreach ($chunks as $chunk) {
  91. $this->stream->write($chunk);
  92. }
  93. $this->stream->flush();
  94. $this->stream->terminate();
  95. $this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__));
  96. }
  97. }