TYPO3 API  SVNRelease
SendmailTransport.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of SwiftMailer.
00005  * (c) 2004-2009 Chris Corbyn
00006  *
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00011 //@require 'Swift/Transport/EsmtpTransport.php';
00012 //@require 'Swift/Transport/IoBuffer.php';
00013 //@require 'Swift/Transport/Log.php';
00014 //@require 'Swift/Events/EventDispatcher.php';
00015 
00016 /**
00017  * SendmailTransport for sending mail through a sendmail/postfix (etc..) binary.
00018  *
00019  * Supported modes are -bs and -t, with any additional flags desired.
00020  * It is advised to use -bs mode since error reporting with -t mode is not
00021  * possible.
00022  *
00023  * @package Swift
00024  * @subpackage Transport
00025  * @author Chris Corbyn
00026  */
00027 class Swift_Transport_SendmailTransport
00028   extends Swift_Transport_AbstractSmtpTransport
00029 {
00030 
00031   /**
00032    * Connection buffer parameters.
00033    * @var array
00034    * @access protected
00035    */
00036   private $_params = array(
00037     'timeout' => 30,
00038     'blocking' => 1,
00039     'command' => '/usr/sbin/sendmail -bs',
00040     'type' => Swift_Transport_IoBuffer::TYPE_PROCESS
00041     );
00042 
00043   /**
00044    * Create a new SendmailTransport with $buf for I/O.
00045    * @param Swift_Transport_IoBuffer $buf
00046    * @param Swift_Events_EventDispatcher $dispatcher
00047    */
00048   public function __construct(Swift_Transport_IoBuffer $buf,
00049     Swift_Events_EventDispatcher $dispatcher)
00050   {
00051     parent::__construct($buf, $dispatcher);
00052   }
00053 
00054   /**
00055    * Start the standalone SMTP session if running in -bs mode.
00056    */
00057   public function start()
00058   {
00059     if (false !== strpos($this->getCommand(), ' -bs'))
00060     {
00061       parent::start();
00062     }
00063   }
00064 
00065   /**
00066    * Set the command to invoke.
00067    * If using -t mode you are strongly advised to include -oi or -i in the
00068    * flags. For example: /usr/sbin/sendmail -oi -t
00069    * Swift will append a -f<sender> flag if one is not present.
00070    * The recommended mode is "-bs" since it is interactive and failure notifications
00071    * are hence possible.
00072    * @param string $command
00073    */
00074   public function setCommand($command)
00075   {
00076     $this->_params['command'] = $command;
00077     return $this;
00078   }
00079 
00080   /**
00081    * Get the sendmail command which will be invoked.
00082    * @return string
00083    */
00084   public function getCommand()
00085   {
00086     return $this->_params['command'];
00087   }
00088 
00089   /**
00090    * Send the given Message.
00091    * Recipient/sender data will be retreived from the Message API.
00092    * The return value is the number of recipients who were accepted for delivery.
00093    * NOTE: If using 'sendmail -t' you will not be aware of any failures until
00094    * they bounce (i.e. send() will always return 100% success).
00095    * @param Swift_Mime_Message $message
00096    * @param string[] &$failedRecipients to collect failures by-reference
00097    * @return int
00098    */
00099   public function send(Swift_Mime_Message $message, &$failedRecipients = null)
00100   {
00101     $failedRecipients = (array) $failedRecipients;
00102     $command = $this->getCommand();
00103     $buffer = $this->getBuffer();
00104 
00105     if (false !== strpos($command, ' -t'))
00106     {
00107       if ($evt = $this->_eventDispatcher->createSendEvent($this, $message))
00108       {
00109         $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
00110         if ($evt->bubbleCancelled())
00111         {
00112           return 0;
00113         }
00114       }
00115 
00116       if (false === strpos($command, ' -f'))
00117       {
00118         $command .= ' -f' . $this->_getReversePath($message);
00119       }
00120 
00121       $buffer->initialize(array_merge($this->_params, array('command' => $command)));
00122 
00123       if (false === strpos($command, ' -i') && false === strpos($command, ' -oi'))
00124       {
00125         $buffer->setWriteTranslations(array("\r\n" => "\n", "\n." => "\n.."));
00126       }
00127       else
00128       {
00129         $buffer->setWriteTranslations(array("\r\n"=>"\n"));
00130       }
00131 
00132       $count = count((array) $message->getTo())
00133         + count((array) $message->getCc())
00134         + count((array) $message->getBcc())
00135         ;
00136       $message->toByteStream($buffer);
00137       $buffer->flushBuffers();
00138       $buffer->setWriteTranslations(array());
00139       $buffer->terminate();
00140 
00141       if ($evt)
00142       {
00143         $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
00144         $evt->setFailedRecipients($failedRecipients);
00145         $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
00146       }
00147 
00148       $message->generateId();
00149     }
00150     elseif (false !== strpos($command, ' -bs'))
00151     {
00152       $count = parent::send($message, $failedRecipients);
00153     }
00154     else
00155     {
00156       $this->_throwException(new Swift_TransportException(
00157         'Unsupported sendmail command flags [' . $command . ']. ' .
00158         'Must be one of "-bs" or "-t" but can include additional flags.'
00159         ));
00160     }
00161 
00162     return $count;
00163   }
00164 
00165   // -- Protected methods
00166 
00167   /** Get the params to initialize the buffer */
00168   protected function _getBufferParams()
00169   {
00170     return $this->_params;
00171   }
00172 
00173 }