vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/SendmailTransport.php line 106

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of SwiftMailer.
  4.  * (c) 2004-2009 Chris Corbyn
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. /**
  10.  * SendmailTransport for sending mail through a Sendmail/Postfix (etc..) binary.
  11.  *
  12.  * Supported modes are -bs and -t, with any additional flags desired.
  13.  * It is advised to use -bs mode since error reporting with -t mode is not
  14.  * possible.
  15.  *
  16.  * @author Chris Corbyn
  17.  */
  18. class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTransport
  19. {
  20.     /**
  21.      * Connection buffer parameters.
  22.      *
  23.      * @var array
  24.      */
  25.     private $params = [
  26.         'timeout' => 30,
  27.         'blocking' => 1,
  28.         'command' => '/usr/sbin/sendmail -bs',
  29.         'type' => Swift_Transport_IoBuffer::TYPE_PROCESS,
  30.         ];
  31.     /**
  32.      * Create a new SendmailTransport with $buf for I/O.
  33.      *
  34.      * @param string $localDomain
  35.      */
  36.     public function __construct(Swift_Transport_IoBuffer $bufSwift_Events_EventDispatcher $dispatcher$localDomain '127.0.0.1'Swift_AddressEncoder $addressEncoder null)
  37.     {
  38.         parent::__construct($buf$dispatcher$localDomain$addressEncoder);
  39.     }
  40.     /**
  41.      * Start the standalone SMTP session if running in -bs mode.
  42.      */
  43.     public function start()
  44.     {
  45.         if (false !== strpos($this->getCommand(), ' -bs')) {
  46.             parent::start();
  47.         }
  48.     }
  49.     /**
  50.      * Set the command to invoke.
  51.      *
  52.      * If using -t mode you are strongly advised to include -oi or -i in the flags.
  53.      * For example: /usr/sbin/sendmail -oi -t
  54.      * Swift will append a -f<sender> flag if one is not present.
  55.      *
  56.      * The recommended mode is "-bs" since it is interactive and failure notifications
  57.      * are hence possible.
  58.      *
  59.      * @param string $command
  60.      *
  61.      * @return $this
  62.      */
  63.     public function setCommand($command)
  64.     {
  65.         $this->params['command'] = $command;
  66.         return $this;
  67.     }
  68.     /**
  69.      * Get the sendmail command which will be invoked.
  70.      *
  71.      * @return string
  72.      */
  73.     public function getCommand()
  74.     {
  75.         return $this->params['command'];
  76.     }
  77.     /**
  78.      * Send the given Message.
  79.      *
  80.      * Recipient/sender data will be retrieved from the Message API.
  81.      *
  82.      * The return value is the number of recipients who were accepted for delivery.
  83.      * NOTE: If using 'sendmail -t' you will not be aware of any failures until
  84.      * they bounce (i.e. send() will always return 100% success).
  85.      *
  86.      * @param string[] $failedRecipients An array of failures by-reference
  87.      *
  88.      * @return int
  89.      */
  90.     public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients null)
  91.     {
  92.         $failedRecipients = (array) $failedRecipients;
  93.         $command $this->getCommand();
  94.         $buffer $this->getBuffer();
  95.         $count 0;
  96.         if (false !== strpos($command' -t')) {
  97.             if ($evt $this->eventDispatcher->createSendEvent($this$message)) {
  98.                 $this->eventDispatcher->dispatchEvent($evt'beforeSendPerformed');
  99.                 if ($evt->bubbleCancelled()) {
  100.                     return 0;
  101.                 }
  102.             }
  103.             if (false === strpos($command' -f')) {
  104.                 $command .= ' -f'.escapeshellarg($this->getReversePath($message) ?? '');
  105.             }
  106.             $buffer->initialize(array_merge($this->params, ['command' => $command]));
  107.             if (false === strpos($command' -i') && false === strpos($command' -oi')) {
  108.                 $buffer->setWriteTranslations(["\r\n" => "\n""\n." => "\n.."]);
  109.             } else {
  110.                 $buffer->setWriteTranslations(["\r\n" => "\n"]);
  111.             }
  112.             $count \count((array) $message->getTo())
  113.                 + \count((array) $message->getCc())
  114.                 + \count((array) $message->getBcc())
  115.                 ;
  116.             $message->toByteStream($buffer);
  117.             $buffer->flushBuffers();
  118.             $buffer->setWriteTranslations([]);
  119.             $buffer->terminate();
  120.             if ($evt) {
  121.                 $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
  122.                 $evt->setFailedRecipients($failedRecipients);
  123.                 $this->eventDispatcher->dispatchEvent($evt'sendPerformed');
  124.             }
  125.             $message->generateId();
  126.         } elseif (false !== strpos($command' -bs')) {
  127.             $count parent::send($message$failedRecipients);
  128.         } else {
  129.             $this->throwException(new Swift_TransportException(
  130.                 'Unsupported sendmail command flags ['.$command.']. '.
  131.                 'Must be one of "-bs" or "-t" but can include additional flags.'
  132.                 ));
  133.         }
  134.         return $count;
  135.     }
  136.     /** Get the params to initialize the buffer */
  137.     protected function getBufferParams()
  138.     {
  139.         return $this->params;
  140.     }
  141. }