TYPO3  7.6
EsmtpTransport.php
Go to the documentation of this file.
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 
17 {
23  private $_handlers = array();
24 
30  private $_capabilities = array();
31 
37  private $_params = array(
38  'protocol' => 'tcp',
39  'host' => 'localhost',
40  'port' => 25,
41  'timeout' => 30,
42  'blocking' => 1,
43  'tls' => false,
45  );
46 
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 
67  public function setHost($host)
68  {
69  $this->_params['host'] = $host;
70 
71  return $this;
72  }
73 
79  public function getHost()
80  {
81  return $this->_params['host'];
82  }
83 
91  public function setPort($port)
92  {
93  $this->_params['port'] = (int) $port;
94 
95  return $this;
96  }
97 
103  public function getPort()
104  {
105  return $this->_params['port'];
106  }
107 
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 
128  public function getTimeout()
129  {
130  return $this->_params['timeout'];
131  }
132 
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 
158  public function getEncryption()
159  {
160  return $this->_params['tls'] ? 'tls' : $this->_params['protocol'];
161  }
162 
170  public function setSourceIp($source)
171  {
172  $this->_params['sourceIp'] = $source;
173 
174  return $this;
175  }
176 
182  public function getSourceIp()
183  {
184  return isset($this->_params['sourceIp']) ? $this->_params['sourceIp'] : null;
185  }
186 
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 
212  public function getExtensionHandlers()
213  {
214  return array_values($this->_handlers);
215  }
216 
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 
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 
268  protected function _getBufferParams()
269  {
270  return $this->_params;
271  }
272 
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 
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 
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 
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 
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 
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 
382  private function _sortHandlers($a, $b)
383  {
384  return $a->getPriorityOver($b->getHandledKeyword());
385  }
386 }