[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Transport/ -> SendmailTransport.php (source)

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


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1