4
* This file is part of SwiftMailer.
5
* (c) 2004-2009 Chris Corbyn
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
11
//@require 'Swift/Transport/AbstractSmtpTransport.php';
12
//@require 'Swift/Transport/EsmtpHandler.php';
13
//@require 'Swift/Transport/IoBuffer.php';
14
//@require 'Swift/Transport/SmtpAgent.php';
15
//@require 'Swift/TransportException.php';
16
//@require 'Swift/Mime/Message.php';
17
//@require 'Swift/Events/EventDispatcher.php';
20
* Sends Messages over SMTP with ESMTP support.
22
* @subpackage Transport
23
* @author Chris Corbyn
25
class Swift_Transport_EsmtpTransport
26
extends Swift_Transport_AbstractSmtpTransport
27
implements Swift_Transport_SmtpAgent
31
* ESMTP extension handlers.
32
* @var Swift_Transport_EsmtpHandler[]
35
private $_handlers = array();
42
private $_capabilities = array();
45
* Connection buffer parameters.
49
private $_params = array(
51
'host' => 'localhost',
55
'type' => Swift_Transport_IoBuffer::TYPE_SOCKET
59
* Creates a new EsmtpTransport using the given I/O buffer.
60
* @param Swift_Transport_IoBuffer $buf
61
* @param Swift_Transport_EsmtpHandler[] $extensionHandlers
62
* @param Swift_Events_EventDispatcher $dispatcher
64
public function __construct(Swift_Transport_IoBuffer $buf,
65
array $extensionHandlers, Swift_Events_EventDispatcher $dispatcher)
67
parent::__construct($buf, $dispatcher);
68
$this->setExtensionHandlers($extensionHandlers);
72
* Set the host to connect to.
75
public function setHost($host)
77
$this->_params['host'] = $host;
82
* Get the host to connect to.
85
public function getHost()
87
return $this->_params['host'];
91
* Set the port to connect to.
94
public function setPort($port)
96
$this->_params['port'] = (int) $port;
101
* Get the port to connect to.
104
public function getPort()
106
return $this->_params['port'];
110
* Set the connection timeout.
111
* @param int $timeout seconds
113
public function setTimeout($timeout)
115
$this->_params['timeout'] = (int) $timeout;
120
* Get the connection timeout.
123
public function getTimeout()
125
return $this->_params['timeout'];
129
* Set the encryption type (tls or ssl)
130
* @param string $encryption
132
public function setEncryption($enc)
134
$this->_params['protocol'] = $enc;
139
* Get the encryption type.
142
public function getEncryption()
144
return $this->_params['protocol'];
148
* Set ESMTP extension handlers.
149
* @param Swift_Transport_EsmtpHandler[] $handlers
151
public function setExtensionHandlers(array $handlers)
154
foreach ($handlers as $handler)
156
$assoc[$handler->getHandledKeyword()] = $handler;
158
uasort($assoc, array($this, '_sortHandlers'));
159
$this->_handlers = $assoc;
160
$this->_setHandlerParams();
165
* Get ESMTP extension handlers.
166
* @return Swift_Transport_EsmtpHandler[]
168
public function getExtensionHandlers()
170
return array_values($this->_handlers);
174
* Run a command against the buffer, expecting the given response codes.
175
* If no response codes are given, the response will not be validated.
176
* If codes are given, an exception will be thrown on an invalid response.
177
* @param string $command
178
* @param int[] $codes
179
* @param string[] &$failures
182
public function executeCommand($command, $codes = array(), &$failures = null)
184
$failures = (array) $failures;
187
foreach ($this->_getActiveHandlers() as $handler)
189
$response = $handler->onCommand(
190
$this, $command, $codes, $failures, $stopSignal
197
return parent::executeCommand($command, $codes, $failures);
200
// -- Mixin invocation code
202
/** Mixin handling method for ESMTP handlers */
203
public function __call($method, $args)
205
foreach ($this->_handlers as $handler)
207
if (in_array(strtolower($method),
208
array_map('strtolower', (array) $handler->exposeMixinMethods())
211
$return = call_user_func_array(array($handler, $method), $args);
212
//Allow fluid method calls
213
if (is_null($return) && substr($method, 0, 3) == 'set')
223
trigger_error('Call to undefined method ' . $method, E_USER_ERROR);
226
// -- Protected methods
228
/** Get the params to initialize the buffer */
229
protected function _getBufferParams()
231
return $this->_params;
234
/** Overridden to perform EHLO instead */
235
protected function _doHeloCommand()
239
$response = $this->executeCommand(
240
sprintf("EHLO %s\r\n", $this->_domain), array(250)
242
$this->_capabilities = $this->_getCapabilities($response);
243
$this->_setHandlerParams();
244
foreach ($this->_getActiveHandlers() as $handler)
246
$handler->afterEhlo($this);
249
catch (Swift_TransportException $e)
251
parent::_doHeloCommand();
255
/** Overridden to add Extension support */
256
protected function _doMailFromCommand($address)
258
$handlers = $this->_getActiveHandlers();
260
foreach ($handlers as $handler)
262
$params = array_merge($params, (array) $handler->getMailParams());
264
$paramStr = !empty($params) ? ' ' . implode(' ', $params) : '';
265
$this->executeCommand(
266
sprintf("MAIL FROM: <%s>%s\r\n", $address, $paramStr), array(250)
270
/** Overridden to add Extension support */
271
protected function _doRcptToCommand($address)
273
$handlers = $this->_getActiveHandlers();
275
foreach ($handlers as $handler)
277
$params = array_merge($params, (array) $handler->getRcptParams());
279
$paramStr = !empty($params) ? ' ' . implode(' ', $params) : '';
280
$this->executeCommand(
281
sprintf("RCPT TO: <%s>%s\r\n", $address, $paramStr), array(250, 251, 252)
285
// -- Private methods
287
/** Determine ESMTP capabilities by function group */
288
private function _getCapabilities($ehloResponse)
290
$capabilities = array();
291
$ehloResponse = trim($ehloResponse);
292
$lines = explode("\r\n", $ehloResponse);
294
foreach ($lines as $line)
296
if (preg_match('/^[0-9]{3}[ -]([A-Z0-9-]+)((?:[ =].*)?)$/Di', $line, $matches))
298
$keyword = strtoupper($matches[1]);
299
$paramStr = strtoupper(ltrim($matches[2], ' ='));
300
$params = !empty($paramStr) ? explode(' ', $paramStr) : array();
301
$capabilities[$keyword] = $params;
304
return $capabilities;
307
/** Set parameters which are used by each extension handler */
308
private function _setHandlerParams()
310
foreach ($this->_handlers as $keyword => $handler)
312
if (array_key_exists($keyword, $this->_capabilities))
314
$handler->setKeywordParams($this->_capabilities[$keyword]);
319
/** Get ESMTP handlers which are currently ok to use */
320
private function _getActiveHandlers()
323
foreach ($this->_handlers as $keyword => $handler)
325
if (array_key_exists($keyword, $this->_capabilities))
327
$handlers[] = $handler;
333
/** Custom sort for extension handler ordering */
334
private function _sortHandlers($a, $b)
336
return $a->getPriorityOver($b->getHandledKeyword());