[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Transport/ -> EsmtpTransport.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 over SMTP with ESMTP support.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTransport implements Swift_Transport_SmtpAgent
  17  {
  18      /**
  19       * ESMTP extension handlers.
  20       *
  21       * @var Swift_Transport_EsmtpHandler[]
  22       */
  23      private $_handlers = array();
  24  
  25      /**
  26       * ESMTP capabilities.
  27       *
  28       * @var string[]
  29       */
  30      private $_capabilities = array();
  31  
  32      /**
  33       * Connection buffer parameters.
  34       *
  35       * @var array
  36       */
  37      private $_params = array(
  38          'protocol' => 'tcp',
  39          'host' => 'localhost',
  40          'port' => 25,
  41          'timeout' => 30,
  42          'blocking' => 1,
  43          'tls' => false,
  44          'type' => Swift_Transport_IoBuffer::TYPE_SOCKET,
  45          );
  46  
  47      /**
  48       * Creates a new EsmtpTransport using the given I/O buffer.
  49       *
  50       * @param Swift_Transport_IoBuffer       $buf
  51       * @param Swift_Transport_EsmtpHandler[] $extensionHandlers
  52       * @param Swift_Events_EventDispatcher   $dispatcher
  53       */
  54      public function __construct(Swift_Transport_IoBuffer $buf, array $extensionHandlers, Swift_Events_EventDispatcher $dispatcher)
  55      {
  56          parent::__construct($buf, $dispatcher);
  57          $this->setExtensionHandlers($extensionHandlers);
  58      }
  59  
  60      /**
  61       * Set the host to connect to.
  62       *
  63       * @param string $host
  64       *
  65       * @return Swift_Transport_EsmtpTransport
  66       */
  67      public function setHost($host)
  68      {
  69          $this->_params['host'] = $host;
  70  
  71          return $this;
  72      }
  73  
  74      /**
  75       * Get the host to connect to.
  76       *
  77       * @return string
  78       */
  79      public function getHost()
  80      {
  81          return $this->_params['host'];
  82      }
  83  
  84      /**
  85       * Set the port to connect to.
  86       *
  87       * @param int $port
  88       *
  89       * @return Swift_Transport_EsmtpTransport
  90       */
  91      public function setPort($port)
  92      {
  93          $this->_params['port'] = (int) $port;
  94  
  95          return $this;
  96      }
  97  
  98      /**
  99       * Get the port to connect to.
 100       *
 101       * @return int
 102       */
 103      public function getPort()
 104      {
 105          return $this->_params['port'];
 106      }
 107  
 108      /**
 109       * Set the connection timeout.
 110       *
 111       * @param int $timeout seconds
 112       *
 113       * @return Swift_Transport_EsmtpTransport
 114       */
 115      public function setTimeout($timeout)
 116      {
 117          $this->_params['timeout'] = (int) $timeout;
 118          $this->_buffer->setParam('timeout', (int) $timeout);
 119  
 120          return $this;
 121      }
 122  
 123      /**
 124       * Get the connection timeout.
 125       *
 126       * @return int
 127       */
 128      public function getTimeout()
 129      {
 130          return $this->_params['timeout'];
 131      }
 132  
 133      /**
 134       * Set the encryption type (tls or ssl).
 135       *
 136       * @param string $encryption
 137       *
 138       * @return Swift_Transport_EsmtpTransport
 139       */
 140      public function setEncryption($encryption)
 141      {
 142          if ('tls' == $encryption) {
 143              $this->_params['protocol'] = 'tcp';
 144              $this->_params['tls'] = true;
 145          } else {
 146              $this->_params['protocol'] = $encryption;
 147              $this->_params['tls'] = false;
 148          }
 149  
 150          return $this;
 151      }
 152  
 153      /**
 154       * Get the encryption type.
 155       *
 156       * @return string
 157       */
 158      public function getEncryption()
 159      {
 160          return $this->_params['tls'] ? 'tls' : $this->_params['protocol'];
 161      }
 162  
 163      /**
 164       * Sets the source IP.
 165       *
 166       * @param string $source
 167       *
 168       * @return Swift_Transport_EsmtpTransport
 169       */
 170      public function setSourceIp($source)
 171      {
 172          $this->_params['sourceIp'] = $source;
 173  
 174          return $this;
 175      }
 176  
 177      /**
 178       * Returns the IP used to connect to the destination.
 179       *
 180       * @return string
 181       */
 182      public function getSourceIp()
 183      {
 184          return isset($this->_params['sourceIp']) ? $this->_params['sourceIp'] : null;
 185      }
 186  
 187      /**
 188       * Set ESMTP extension handlers.
 189       *
 190       * @param Swift_Transport_EsmtpHandler[] $handlers
 191       *
 192       * @return Swift_Transport_EsmtpTransport
 193       */
 194      public function setExtensionHandlers(array $handlers)
 195      {
 196          $assoc = array();
 197          foreach ($handlers as $handler) {
 198              $assoc[$handler->getHandledKeyword()] = $handler;
 199          }
 200          uasort($assoc, array($this, '_sortHandlers'));
 201          $this->_handlers = $assoc;
 202          $this->_setHandlerParams();
 203  
 204          return $this;
 205      }
 206  
 207      /**
 208       * Get ESMTP extension handlers.
 209       *
 210       * @return Swift_Transport_EsmtpHandler[]
 211       */
 212      public function getExtensionHandlers()
 213      {
 214          return array_values($this->_handlers);
 215      }
 216  
 217      /**
 218       * Run a command against the buffer, expecting the given response codes.
 219       *
 220       * If no response codes are given, the response will not be validated.
 221       * If codes are given, an exception will be thrown on an invalid response.
 222       *
 223       * @param string   $command
 224       * @param int[]    $codes
 225       * @param string[] $failures An array of failures by-reference
 226       *
 227       * @return string
 228       */
 229      public function executeCommand($command, $codes = array(), &$failures = null)
 230      {
 231          $failures = (array) $failures;
 232          $stopSignal = false;
 233          $response = null;
 234          foreach ($this->_getActiveHandlers() as $handler) {
 235              $response = $handler->onCommand(
 236                  $this, $command, $codes, $failures, $stopSignal
 237                  );
 238              if ($stopSignal) {
 239                  return $response;
 240              }
 241          }
 242  
 243          return parent::executeCommand($command, $codes, $failures);
 244      }
 245  
 246      // -- Mixin invocation code
 247  
 248      /** Mixin handling method for ESMTP handlers */
 249      public function __call($method, $args)
 250      {
 251          foreach ($this->_handlers as $handler) {
 252              if (in_array(strtolower($method),
 253                  array_map('strtolower', (array) $handler->exposeMixinMethods())
 254                  )) {
 255                  $return = call_user_func_array(array($handler, $method), $args);
 256                  // Allow fluid method calls
 257                  if (is_null($return) && substr($method, 0, 3) == 'set') {
 258                      return $this;
 259                  } else {
 260                      return $return;
 261                  }
 262              }
 263          }
 264          trigger_error('Call to undefined method '.$method, E_USER_ERROR);
 265      }
 266  
 267      /** Get the params to initialize the buffer */
 268      protected function _getBufferParams()
 269      {
 270          return $this->_params;
 271      }
 272  
 273      /** Overridden to perform EHLO instead */
 274      protected function _doHeloCommand()
 275      {
 276          try {
 277              $response = $this->executeCommand(
 278                  sprintf("EHLO %s\r\n", $this->_domain), array(250)
 279                  );
 280          } catch (Swift_TransportException $e) {
 281              return parent::_doHeloCommand();
 282          }
 283  
 284          if ($this->_params['tls']) {
 285              try {
 286                  $this->executeCommand("STARTTLS\r\n", array(220));
 287  
 288                  if (!$this->_buffer->startTLS()) {
 289                      throw new Swift_TransportException('Unable to connect with TLS encryption');
 290                  }
 291  
 292                  try {
 293                      $response = $this->executeCommand(
 294                          sprintf("EHLO %s\r\n", $this->_domain), array(250)
 295                          );
 296                  } catch (Swift_TransportException $e) {
 297                      return parent::_doHeloCommand();
 298                  }
 299              } catch (Swift_TransportException $e) {
 300                  $this->_throwException($e);
 301              }
 302          }
 303  
 304          $this->_capabilities = $this->_getCapabilities($response);
 305          $this->_setHandlerParams();
 306          foreach ($this->_getActiveHandlers() as $handler) {
 307              $handler->afterEhlo($this);
 308          }
 309      }
 310  
 311      /** Overridden to add Extension support */
 312      protected function _doMailFromCommand($address)
 313      {
 314          $handlers = $this->_getActiveHandlers();
 315          $params = array();
 316          foreach ($handlers as $handler) {
 317              $params = array_merge($params, (array) $handler->getMailParams());
 318          }
 319          $paramStr = !empty($params) ? ' '.implode(' ', $params) : '';
 320          $this->executeCommand(
 321              sprintf("MAIL FROM:<%s>%s\r\n", $address, $paramStr), array(250)
 322              );
 323      }
 324  
 325      /** Overridden to add Extension support */
 326      protected function _doRcptToCommand($address)
 327      {
 328          $handlers = $this->_getActiveHandlers();
 329          $params = array();
 330          foreach ($handlers as $handler) {
 331              $params = array_merge($params, (array) $handler->getRcptParams());
 332          }
 333          $paramStr = !empty($params) ? ' '.implode(' ', $params) : '';
 334          $this->executeCommand(
 335              sprintf("RCPT TO:<%s>%s\r\n", $address, $paramStr), array(250, 251, 252)
 336              );
 337      }
 338  
 339      /** Determine ESMTP capabilities by function group */
 340      private function _getCapabilities($ehloResponse)
 341      {
 342          $capabilities = array();
 343          $ehloResponse = trim($ehloResponse);
 344          $lines = explode("\r\n", $ehloResponse);
 345          array_shift($lines);
 346          foreach ($lines as $line) {
 347              if (preg_match('/^[0-9]{3}[ -]([A-Z0-9-]+)((?:[ =].*)?)$/Di', $line, $matches)) {
 348                  $keyword = strtoupper($matches[1]);
 349                  $paramStr = strtoupper(ltrim($matches[2], ' ='));
 350                  $params = !empty($paramStr) ? explode(' ', $paramStr) : array();
 351                  $capabilities[$keyword] = $params;
 352              }
 353          }
 354  
 355          return $capabilities;
 356      }
 357  
 358      /** Set parameters which are used by each extension handler */
 359      private function _setHandlerParams()
 360      {
 361          foreach ($this->_handlers as $keyword => $handler) {
 362              if (array_key_exists($keyword, $this->_capabilities)) {
 363                  $handler->setKeywordParams($this->_capabilities[$keyword]);
 364              }
 365          }
 366      }
 367  
 368      /** Get ESMTP handlers which are currently ok to use */
 369      private function _getActiveHandlers()
 370      {
 371          $handlers = array();
 372          foreach ($this->_handlers as $keyword => $handler) {
 373              if (array_key_exists($keyword, $this->_capabilities)) {
 374                  $handlers[] = $handler;
 375              }
 376          }
 377  
 378          return $handlers;
 379      }
 380  
 381      /** Custom sort for extension handler ordering */
 382      private function _sortHandlers($a, $b)
 383      {
 384          return $a->getPriorityOver($b->getHandledKeyword());
 385      }
 386  }


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