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