SendmailTransport.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\EventDispatcher\EventDispatcherInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Mailer\Envelope;
  14. use Symfony\Component\Mailer\SentMessage;
  15. use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
  16. use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
  17. use Symfony\Component\Mailer\Transport\Smtp\Stream\ProcessStream;
  18. use Symfony\Component\Mime\RawMessage;
  19. /**
  20. * SendmailTransport for sending mail through a Sendmail/Postfix (etc..) binary.
  21. *
  22. * Transport can be instantiated through SendmailTransportFactory or NativeTransportFactory:
  23. *
  24. * - SendmailTransportFactory to use most common sendmail path and recommended options
  25. * - NativeTransportFactory when configuration is set via php.ini
  26. *
  27. * @author Fabien Potencier <fabien@symfony.com>
  28. * @author Chris Corbyn
  29. */
  30. class SendmailTransport extends AbstractTransport
  31. {
  32. private string $command = '/usr/sbin/sendmail -bs';
  33. private ProcessStream $stream;
  34. private ?SmtpTransport $transport = null;
  35. /**
  36. * Constructor.
  37. *
  38. * Supported modes are -bs and -t, with any additional flags desired.
  39. *
  40. * The recommended mode is "-bs" since it is interactive and failure notifications are hence possible.
  41. * Note that the -t mode does not support error reporting and does not support Bcc properly (the Bcc headers are not removed).
  42. *
  43. * If using -t mode, you are strongly advised to include -oi or -i in the flags (like /usr/sbin/sendmail -oi -t)
  44. *
  45. * -f<sender> flag will be appended automatically if one is not present.
  46. */
  47. public function __construct(?string $command = null, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null)
  48. {
  49. parent::__construct($dispatcher, $logger);
  50. if (null !== $command) {
  51. if (!str_contains($command, ' -bs') && !str_contains($command, ' -t')) {
  52. throw new \InvalidArgumentException(sprintf('Unsupported sendmail command flags "%s"; must be one of "-bs" or "-t" but can include additional flags.', $command));
  53. }
  54. $this->command = $command;
  55. }
  56. $this->stream = new ProcessStream();
  57. if (str_contains($this->command, ' -bs')) {
  58. $this->stream->setCommand($this->command);
  59. $this->stream->setInteractive(true);
  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 ($recipients = $message->getEnvelope()->getRecipients()) {
  82. $command = str_replace(' -t', '', $command);
  83. }
  84. if (!str_contains($command, ' -f')) {
  85. $command .= ' -f'.escapeshellarg($message->getEnvelope()->getSender()->getEncodedAddress());
  86. }
  87. $chunks = AbstractStream::replace("\r\n", "\n", $message->toIterable());
  88. if (!str_contains($command, ' -i') && !str_contains($command, ' -oi')) {
  89. $chunks = AbstractStream::replace("\n.", "\n..", $chunks);
  90. }
  91. foreach ($recipients as $recipient) {
  92. $command .= ' '.escapeshellarg($recipient->getEncodedAddress());
  93. }
  94. $this->stream->setCommand($command);
  95. $this->stream->initialize();
  96. foreach ($chunks as $chunk) {
  97. $this->stream->write($chunk);
  98. }
  99. $this->stream->flush();
  100. $this->stream->terminate();
  101. $this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__));
  102. }
  103. }