8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
18
* @subpackage Protocol
19
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
20
* @license http://framework.zend.com/license/new-bsd New BSD License
21
* @version $Id: Smtp.php 11196 2008-09-02 00:56:25Z yoshida@zend.co.jp $
28
require_once 'Zend/Mime.php';
32
* @see Zend_Mail_Protocol_Abstract
34
require_once 'Zend/Mail/Protocol/Abstract.php';
38
* Smtp implementation of Zend_Mail_Protocol_Abstract
40
* Minimum implementation according to RFC2821: EHLO, MAIL FROM, RCPT TO, DATA, RSET, NOOP, QUIT
44
* @subpackage Protocol
45
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
46
* @license http://framework.zend.com/license/new-bsd New BSD License
48
class Zend_Mail_Protocol_Smtp extends Zend_Mail_Protocol_Abstract
51
* The transport method for the socket
55
protected $_transport = 'tcp';
59
* Indicates that a session is requested to be secure
67
* Indicates an smtp session has been started by the HELO command
71
protected $_sess = false;
75
* Indicates the HELO command has been issues
79
protected $_helo = false;
83
* Indicates an smtp AUTH has been issued and authenticated
87
protected $_auth = false;
91
* Indicates a MAIL command has been issued
95
protected $_mail = false;
99
* Indicates one or more RCTP commands have been issued
103
protected $_rcpt = false;
107
* Indicates that DATA has been issued and sent
111
protected $_data = null;
117
* @param string $host
118
* @param integer $port
119
* @param array $config
121
* @throws Zend_Mail_Protocol_Exception
123
public function __construct($host = '127.0.0.1', $port = null, array $config = array())
125
if (isset($config['ssl'])) {
126
switch (strtolower($config['ssl'])) {
128
$this->_secure = 'tls';
132
$this->_transport = 'ssl';
133
$this->_secure = 'ssl';
141
* @see Zend_Mail_Protocol_Exception
143
require_once 'Zend/Mail/Protocol/Exception.php';
144
throw new Zend_Mail_Protocol_Exception($config['ssl'] . ' is unsupported SSL type');
149
// If no port has been specified then check the master PHP ini file. Defaults to 25 if the ini setting is null.
151
if (($port = ini_get('smtp_port')) == '') {
156
parent::__construct($host, $port);
161
* Connect to the server with the parameters given in the constructor.
165
public function connect()
167
return $this->_connect($this->_transport . '://' . $this->_host . ':'. $this->_port);
172
* Initiate HELO/EHLO sequence and set flag to indicate valid smtp session
174
* @param string $host The client hostname or IP address (default: 127.0.0.1)
175
* @throws Zend_Mail_Protocol_Exception
178
public function helo($host = '127.0.0.1')
180
// Respect RFC 2821 and disallow HELO attempts if session is already initiated.
181
if ($this->_sess === true) {
183
* @see Zend_Mail_Protocol_Exception
185
require_once 'Zend/Mail/Protocol/Exception.php';
186
throw new Zend_Mail_Protocol_Exception('Cannot issue HELO to existing session');
189
// Validate client hostname
190
if (!$this->_validHost->isValid($host)) {
192
* @see Zend_Mail_Protocol_Exception
194
require_once 'Zend/Mail/Protocol/Exception.php';
195
throw new Zend_Mail_Protocol_Exception(join(', ', $this->_validHost->getMessages()));
198
// Initiate helo sequence
199
$this->_expect(220, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
202
// If a TLS session is required, commence negotiation
203
if ($this->_secure == 'tls') {
204
$this->_send('STARTTLS');
205
$this->_expect(220, 180);
206
if (!stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
208
* @see Zend_Mail_Protocol_Exception
210
require_once 'Zend/Mail/Protocol/Exception.php';
211
throw new Zend_Mail_Protocol_Exception('Unable to connect via TLS');
216
$this->_startSession();
222
* Send EHLO or HELO depending on capabilities of smtp host
224
* @param string $host The client hostname or IP address (default: 127.0.0.1)
225
* @throws Zend_Mail_Protocol_Exception
228
protected function _ehlo($host)
230
// Support for older, less-compliant remote servers. Tries multiple attempts of EHLO or HELO.
232
$this->_send('EHLO ' . $host);
233
$this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
234
} catch (Zend_Mail_Protocol_Exception $e) {
235
$this->_send('HELO ' . $host);
236
$this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
237
} catch (Zend_Mail_Protocol_Exception $e) {
244
* Issues MAIL command
246
* @param string $from Sender mailbox
247
* @throws Zend_Mail_Protocol_Exception
250
public function mail($from)
252
if ($this->_sess !== true) {
254
* @see Zend_Mail_Protocol_Exception
256
require_once 'Zend/Mail/Protocol/Exception.php';
257
throw new Zend_Mail_Protocol_Exception('A valid session has not been started');
260
$this->_send('MAIL FROM:<' . $from . '>');
261
$this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
263
// Set mail to true, clear recipients and any existing data flags as per 4.1.1.2 of RFC 2821
265
$this->_rcpt = false;
266
$this->_data = false;
271
* Issues RCPT command
273
* @param string $to Receiver(s) mailbox
274
* @throws Zend_Mail_Protocol_Exception
277
public function rcpt($to)
279
if ($this->_mail !== true) {
281
* @see Zend_Mail_Protocol_Exception
283
require_once 'Zend/Mail/Protocol/Exception.php';
284
throw new Zend_Mail_Protocol_Exception('No sender reverse path has been supplied');
287
// Set rcpt to true, as per 4.1.1.3 of RFC 2821
288
$this->_send('RCPT TO:<' . $to . '>');
289
$this->_expect(array(250, 251), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
295
* Issues DATA command
297
* @param string $data
298
* @throws Zend_Mail_Protocol_Exception
301
public function data($data)
303
// Ensure recipients have been set
304
if ($this->_rcpt !== true) {
306
* @see Zend_Mail_Protocol_Exception
308
require_once 'Zend/Mail/Protocol/Exception.php';
309
throw new Zend_Mail_Protocol_Exception('No recipient forward path has been supplied');
312
$this->_send('DATA');
313
$this->_expect(354, 120); // Timeout set for 2 minutes as per RFC 2821 4.5.3.2
315
foreach (explode(Zend_Mime::LINEEND, $data) as $line) {
316
if (strpos($line, '.') === 0) {
317
// Escape lines prefixed with a '.'
324
$this->_expect(250, 600); // Timeout set for 10 minutes as per RFC 2821 4.5.3.2
330
* Issues the RSET command end validates answer
332
* Can be used to restore a clean smtp communication state when a transaction has been cancelled or commencing a new transaction.
336
public function rset()
338
$this->_send('RSET');
339
// MS ESMTP doesn't follow RFC, see [ZF-1377]
340
$this->_expect(array(250, 220));
342
$this->_mail = false;
343
$this->_rcpt = false;
344
$this->_data = false;
349
* Issues the NOOP command end validates answer
351
* Not used by Zend_Mail, could be used to keep a connection alive or check if it is still open.
355
public function noop()
357
$this->_send('NOOP');
358
$this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
363
* Issues the VRFY command end validates answer
365
* Not used by Zend_Mail.
367
* @param string $user User Name or eMail to verify
370
public function vrfy($user)
372
$this->_send('VRFY ' . $user);
373
$this->_expect(array(250, 251, 252), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
378
* Issues the QUIT command and clears the current session
382
public function quit()
385
$this->_send('QUIT');
386
$this->_expect(221, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
387
$this->_stopSession();
393
* Default authentication method
395
* This default method is implemented by AUTH adapters to properly authenticate to a remote host.
397
* @throws Zend_Mail_Protocol_Exception
400
public function auth()
402
if ($this->_auth === true) {
404
* @see Zend_Mail_Protocol_Exception
406
require_once 'Zend/Mail/Protocol/Exception.php';
407
throw new Zend_Mail_Protocol_Exception('Already authenticated for this session');
417
public function disconnect()
419
$this->_disconnect();
428
protected function _startSession()
439
protected function _stopSession()
441
$this->_sess = false;