vendor/swiftmailer/lib/classes/Swift/Transport/MailTransport.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  * Sends Messages using the mail() function.
       
    14  * 
       
    15  * It is advised that users do not use this transport if at all possible
       
    16  * since a number of plugin features cannot be used in conjunction with this
       
    17  * transport due to the internal interface in PHP itself.
       
    18  * 
       
    19  * The level of error reporting with this transport is incredibly weak, again
       
    20  * due to limitations of PHP's internal mail() function.  You'll get an
       
    21  * all-or-nothing result from sending.
       
    22  * 
       
    23  * @package Swift
       
    24  * @subpackage Transport
       
    25  * @author Chris Corbyn
       
    26  */
       
    27 class Swift_Transport_MailTransport implements Swift_Transport
       
    28 {
       
    29 
       
    30   /** Addtional parameters to pass to mail() */
       
    31   private $_extraParams = '-f%s';
       
    32   
       
    33   /** The event dispatcher from the plugin API */
       
    34   private $_eventDispatcher;
       
    35   
       
    36   /** An invoker that calls the mail() function */
       
    37   private $_invoker;
       
    38   
       
    39   /**
       
    40    * Create a new MailTransport with the $log.
       
    41    * @param Swift_Transport_Log $log
       
    42    */
       
    43   public function __construct(Swift_Transport_MailInvoker $invoker,
       
    44     Swift_Events_EventDispatcher $eventDispatcher)
       
    45   {
       
    46     $this->_invoker = $invoker;
       
    47     $this->_eventDispatcher = $eventDispatcher;
       
    48   }
       
    49   
       
    50   /**
       
    51    * Not used.
       
    52    */
       
    53   public function isStarted()
       
    54   {
       
    55     return false;
       
    56   }
       
    57   
       
    58   /**
       
    59    * Not used.
       
    60    */
       
    61   public function start()
       
    62   {
       
    63   }
       
    64   
       
    65   /**
       
    66    * Not used.
       
    67    */
       
    68   public function stop()
       
    69   {
       
    70   }
       
    71   
       
    72   /**
       
    73    * Set the additional parameters used on the mail() function.
       
    74    * 
       
    75    * This string is formatted for sprintf() where %s is the sender address.
       
    76    * 
       
    77    * @param string $params
       
    78    */
       
    79   public function setExtraParams($params)
       
    80   {
       
    81     $this->_extraParams = $params;
       
    82     return $this;
       
    83   }
       
    84   
       
    85   /**
       
    86    * Get the additional parameters used on the mail() function.
       
    87    * 
       
    88    * This string is formatted for sprintf() where %s is the sender address.
       
    89    * 
       
    90    * @return string
       
    91    */
       
    92   public function getExtraParams()
       
    93   {
       
    94     return $this->_extraParams;
       
    95   }
       
    96   
       
    97   /**
       
    98    * Send the given Message.
       
    99    * 
       
   100    * Recipient/sender data will be retreived from the Message API.
       
   101    * The return value is the number of recipients who were accepted for delivery.
       
   102    * 
       
   103    * @param Swift_Mime_Message $message
       
   104    * @param string[] &$failedRecipients to collect failures by-reference
       
   105    * @return int
       
   106    */
       
   107   public function send(Swift_Mime_Message $message, &$failedRecipients = null)
       
   108   {
       
   109     $failedRecipients = (array) $failedRecipients;
       
   110     
       
   111     if ($evt = $this->_eventDispatcher->createSendEvent($this, $message))
       
   112     {
       
   113       $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
       
   114       if ($evt->bubbleCancelled())
       
   115       {
       
   116         return 0;
       
   117       }
       
   118     }
       
   119     
       
   120     $count = (
       
   121       count((array) $message->getTo())
       
   122       + count((array) $message->getCc())
       
   123       + count((array) $message->getBcc())
       
   124       );
       
   125     
       
   126     $toHeader = $message->getHeaders()->get('To');
       
   127     $subjectHeader = $message->getHeaders()->get('Subject');
       
   128     
       
   129     $to = $toHeader->getFieldBody();
       
   130     $subject = $subjectHeader->getFieldBody();
       
   131     
       
   132     $reversePath = $this->_getReversePath($message);
       
   133     
       
   134     //Remove headers that would otherwise be duplicated
       
   135     $message->getHeaders()->remove('To');
       
   136     $message->getHeaders()->remove('Subject');
       
   137     
       
   138     $messageStr = $message->toString();
       
   139     
       
   140     $message->getHeaders()->set($toHeader);
       
   141     $message->getHeaders()->set($subjectHeader);
       
   142     
       
   143     //Separate headers from body
       
   144     if (false !== $endHeaders = strpos($messageStr, "\r\n\r\n"))
       
   145     {
       
   146       $headers = substr($messageStr, 0, $endHeaders) . "\r\n"; //Keep last EOL
       
   147       $body = substr($messageStr, $endHeaders + 4);
       
   148     }
       
   149     else
       
   150     {
       
   151       $headers = $messageStr . "\r\n";
       
   152       $body = '';
       
   153     }
       
   154     
       
   155     unset($messageStr);
       
   156     
       
   157     if ("\r\n" != PHP_EOL) //Non-windows (not using SMTP)
       
   158     {
       
   159       $headers = str_replace("\r\n", PHP_EOL, $headers);
       
   160       $body = str_replace("\r\n", PHP_EOL, $body);
       
   161     }
       
   162     else //Windows, using SMTP
       
   163     {
       
   164       $headers = str_replace("\r\n.", "\r\n..", $headers);
       
   165       $body = str_replace("\r\n.", "\r\n..", $body);
       
   166     }
       
   167     
       
   168     if ($this->_invoker->mail($to, $subject, $body, $headers,
       
   169       sprintf($this->_extraParams, $reversePath)))
       
   170     {
       
   171       if ($evt)
       
   172       {
       
   173         $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
       
   174         $evt->setFailedRecipients($failedRecipients);
       
   175         $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
       
   176       }
       
   177     }
       
   178     else
       
   179     {
       
   180       $failedRecipients = array_merge(
       
   181         $failedRecipients,
       
   182         array_keys((array) $message->getTo()),
       
   183         array_keys((array) $message->getCc()),
       
   184         array_keys((array) $message->getBcc())
       
   185         );
       
   186       
       
   187       if ($evt)
       
   188       {
       
   189         $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED);
       
   190         $evt->setFailedRecipients($failedRecipients);
       
   191         $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
       
   192       }
       
   193       
       
   194       $message->generateId();
       
   195       
       
   196       $count = 0;
       
   197     }
       
   198     
       
   199     return $count;
       
   200   }
       
   201   
       
   202   /**
       
   203    * Register a plugin.
       
   204    * 
       
   205    * @param Swift_Events_EventListener $plugin
       
   206    */
       
   207   public function registerPlugin(Swift_Events_EventListener $plugin)
       
   208   {
       
   209     $this->_eventDispatcher->bindEventListener($plugin);
       
   210   }
       
   211   
       
   212   // -- Private methods
       
   213   
       
   214   /** Determine the best-use reverse path for this message */
       
   215   private function _getReversePath(Swift_Mime_Message $message)
       
   216   {
       
   217     $return = $message->getReturnPath();
       
   218     $sender = $message->getSender();
       
   219     $from = $message->getFrom();
       
   220     $path = null;
       
   221     if (!empty($return))
       
   222     {
       
   223       $path = $return;
       
   224     }
       
   225     elseif (!empty($sender))
       
   226     {
       
   227       $keys = array_keys($sender);
       
   228       $path = array_shift($keys);
       
   229     }
       
   230     elseif (!empty($from))
       
   231     {
       
   232       $keys = array_keys($from);
       
   233       $path = array_shift($keys);
       
   234     }
       
   235     return $path;
       
   236   }
       
   237   
       
   238 }