[ Index ]

MailPress 544

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

title

Body

[close]

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


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