TYPO3 API  SVNRelease
MailTransport.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.php';
00012 //@require 'Swift/Transport/MailInvoker.php';
00013 //@require 'Swift/Mime/Message.php';
00014 //@require 'Swift/Events/EventListener.php';
00015 
00016 /**
00017  * Sends Messages using the mail() function.
00018  *
00019  * It is advised that users do not use this transport if at all possible
00020  * since a number of plugin features cannot be used in conjunction with this
00021  * transport due to the internal interface in PHP itself.
00022  *
00023  * The level of error reporting with this transport is incredibly weak, again
00024  * due to limitations of PHP's internal mail() function.  You'll get an
00025  * all-or-nothing result from sending.
00026  *
00027  * @package Swift
00028  * @subpackage Transport
00029  * @author Chris Corbyn
00030  */
00031 class Swift_Transport_MailTransport implements Swift_Transport
00032 {
00033 
00034   /** Addtional parameters to pass to mail() */
00035   private $_extraParams = '-f%s';
00036 
00037   /** The event dispatcher from the plugin API */
00038   private $_eventDispatcher;
00039 
00040   /** An invoker that calls the mail() function */
00041   private $_invoker;
00042 
00043   /**
00044    * Create a new MailTransport with the $log.
00045    * @param Swift_Transport_Log $log
00046    */
00047   public function __construct(Swift_Transport_MailInvoker $invoker,
00048     Swift_Events_EventDispatcher $eventDispatcher)
00049   {
00050     $this->_invoker = $invoker;
00051     $this->_eventDispatcher = $eventDispatcher;
00052   }
00053 
00054   /**
00055    * Not used.
00056    */
00057   public function isStarted()
00058   {
00059     return false;
00060   }
00061 
00062   /**
00063    * Not used.
00064    */
00065   public function start()
00066   {
00067   }
00068 
00069   /**
00070    * Not used.
00071    */
00072   public function stop()
00073   {
00074   }
00075 
00076   /**
00077    * Set the additional parameters used on the mail() function.
00078    *
00079    * This string is formatted for sprintf() where %s is the sender address.
00080    *
00081    * @param string $params
00082    */
00083   public function setExtraParams($params)
00084   {
00085     $this->_extraParams = $params;
00086     return $this;
00087   }
00088 
00089   /**
00090    * Get the additional parameters used on the mail() function.
00091    *
00092    * This string is formatted for sprintf() where %s is the sender address.
00093    *
00094    * @return string
00095    */
00096   public function getExtraParams()
00097   {
00098     return $this->_extraParams;
00099   }
00100 
00101   /**
00102    * Send the given Message.
00103    *
00104    * Recipient/sender data will be retreived from the Message API.
00105    * The return value is the number of recipients who were accepted for delivery.
00106    *
00107    * @param Swift_Mime_Message $message
00108    * @param string[] &$failedRecipients to collect failures by-reference
00109    * @return int
00110    */
00111   public function send(Swift_Mime_Message $message, &$failedRecipients = null)
00112   {
00113     $failedRecipients = (array) $failedRecipients;
00114 
00115     if ($evt = $this->_eventDispatcher->createSendEvent($this, $message))
00116     {
00117       $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
00118       if ($evt->bubbleCancelled())
00119       {
00120         return 0;
00121       }
00122     }
00123 
00124     $count = (
00125       count((array) $message->getTo())
00126       + count((array) $message->getCc())
00127       + count((array) $message->getBcc())
00128       );
00129 
00130     $toHeader = $message->getHeaders()->get('To');
00131     $subjectHeader = $message->getHeaders()->get('Subject');
00132 
00133     $to = $toHeader->getFieldBody();
00134     $subject = $subjectHeader->getFieldBody();
00135 
00136     $reversePath = $this->_getReversePath($message);
00137 
00138     //Remove headers that would otherwise be duplicated
00139     $message->getHeaders()->remove('To');
00140     $message->getHeaders()->remove('Subject');
00141 
00142     $messageStr = $message->toString();
00143 
00144     $message->getHeaders()->set($toHeader);
00145     $message->getHeaders()->set($subjectHeader);
00146 
00147     //Separate headers from body
00148     if (false !== $endHeaders = strpos($messageStr, "\r\n\r\n"))
00149     {
00150       $headers = substr($messageStr, 0, $endHeaders) . "\r\n"; //Keep last EOL
00151       $body = substr($messageStr, $endHeaders + 4);
00152     }
00153     else
00154     {
00155       $headers = $messageStr . "\r\n";
00156       $body = '';
00157     }
00158 
00159     unset($messageStr);
00160 
00161     if ("\r\n" != PHP_EOL) //Non-windows (not using SMTP)
00162     {
00163       $headers = str_replace("\r\n", PHP_EOL, $headers);
00164       $body = str_replace("\r\n", PHP_EOL, $body);
00165     }
00166     else //Windows, using SMTP
00167     {
00168       $headers = str_replace("\r\n.", "\r\n..", $headers);
00169       $body = str_replace("\r\n.", "\r\n..", $body);
00170     }
00171 
00172     if ($this->_invoker->mail($to, $subject, $body, $headers,
00173       sprintf($this->_extraParams, $reversePath)))
00174     {
00175       if ($evt)
00176       {
00177         $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
00178         $evt->setFailedRecipients($failedRecipients);
00179         $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
00180       }
00181     }
00182     else
00183     {
00184       $failedRecipients = array_merge(
00185         $failedRecipients,
00186         array_keys((array) $message->getTo()),
00187         array_keys((array) $message->getCc()),
00188         array_keys((array) $message->getBcc())
00189         );
00190 
00191       if ($evt)
00192       {
00193         $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED);
00194         $evt->setFailedRecipients($failedRecipients);
00195         $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
00196       }
00197 
00198       $message->generateId();
00199 
00200       $count = 0;
00201     }
00202 
00203     return $count;
00204   }
00205 
00206   /**
00207    * Register a plugin.
00208    *
00209    * @param Swift_Events_EventListener $plugin
00210    */
00211   public function registerPlugin(Swift_Events_EventListener $plugin)
00212   {
00213     $this->_eventDispatcher->bindEventListener($plugin);
00214   }
00215 
00216   // -- Private methods
00217 
00218   /** Determine the best-use reverse path for this message */
00219   private function _getReversePath(Swift_Mime_Message $message)
00220   {
00221     $return = $message->getReturnPath();
00222     $sender = $message->getSender();
00223     $from = $message->getFrom();
00224     $path = null;
00225     if (!empty($return))
00226     {
00227       $path = $return;
00228     }
00229     elseif (!empty($sender))
00230     {
00231       $keys = array_keys($sender);
00232       $path = array_shift($keys);
00233     }
00234     elseif (!empty($from))
00235     {
00236       $keys = array_keys($from);
00237       $path = array_shift($keys);
00238     }
00239     return $path;
00240   }
00241 
00242 }