00001 <?php
00002
00028 require_once 'Zend/Mime.php';
00029
00030
00034 require_once 'Zend/Mail/Protocol/Abstract.php';
00035
00036
00048 class Zend_Mail_Protocol_Smtp extends Zend_Mail_Protocol_Abstract
00049 {
00055 protected $_transport = 'tcp';
00056
00057
00063 protected $_secure;
00064
00065
00071 protected $_sess = false;
00072
00073
00079 protected $_helo = false;
00080
00081
00087 protected $_auth = false;
00088
00089
00095 protected $_mail = false;
00096
00097
00103 protected $_rcpt = false;
00104
00105
00111 protected $_data = null;
00112
00113
00123 public function __construct($host = '127.0.0.1', $port = null, array $config = array())
00124 {
00125 if (isset($config['ssl'])) {
00126 switch (strtolower($config['ssl'])) {
00127 case 'tls':
00128 $this->_secure = 'tls';
00129 break;
00130
00131 case 'ssl':
00132 $this->_transport = 'ssl';
00133 $this->_secure = 'ssl';
00134 if ($port == null) {
00135 $port = 465;
00136 }
00137 break;
00138
00139 default:
00143 require_once 'Zend/Mail/Protocol/Exception.php';
00144 throw new Zend_Mail_Protocol_Exception($config['ssl'] . ' is unsupported SSL type');
00145 break;
00146 }
00147 }
00148
00149
00150 if ($port == null) {
00151 if (($port = ini_get('smtp_port')) == '') {
00152 $port = 25;
00153 }
00154 }
00155
00156 parent::__construct($host, $port);
00157 }
00158
00159
00165 public function connect()
00166 {
00167 return $this->_connect($this->_transport . '://' . $this->_host . ':'. $this->_port);
00168 }
00169
00170
00178 public function helo($host = '127.0.0.1')
00179 {
00180
00181 if ($this->_sess === true) {
00185 require_once 'Zend/Mail/Protocol/Exception.php';
00186 throw new Zend_Mail_Protocol_Exception('Cannot issue HELO to existing session');
00187 }
00188
00189
00190 if (!$this->_validHost->isValid($host)) {
00194 require_once 'Zend/Mail/Protocol/Exception.php';
00195 throw new Zend_Mail_Protocol_Exception(join(', ', $this->_validHost->getMessages()));
00196 }
00197
00198
00199 $this->_expect(220, 300);
00200 $this->_ehlo($host);
00201
00202
00203 if ($this->_secure == 'tls') {
00204 $this->_send('STARTTLS');
00205 $this->_expect(220, 180);
00206 if (!stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
00210 require_once 'Zend/Mail/Protocol/Exception.php';
00211 throw new Zend_Mail_Protocol_Exception('Unable to connect via TLS');
00212 }
00213 $this->_ehlo($host);
00214 }
00215
00216 $this->_startSession();
00217 $this->auth();
00218 }
00219
00220
00228 protected function _ehlo($host)
00229 {
00230
00231 try {
00232 $this->_send('EHLO ' . $host);
00233 $this->_expect(250, 300);
00234 } catch (Zend_Mail_Protocol_Exception $e) {
00235 $this->_send('HELO ' . $host);
00236 $this->_expect(250, 300);
00237 } catch (Zend_Mail_Protocol_Exception $e) {
00238 throw $e;
00239 }
00240 }
00241
00242
00250 public function mail($from)
00251 {
00252 if ($this->_sess !== true) {
00256 require_once 'Zend/Mail/Protocol/Exception.php';
00257 throw new Zend_Mail_Protocol_Exception('A valid session has not been started');
00258 }
00259
00260 $this->_send('MAIL FROM:<' . $from . '>');
00261 $this->_expect(250, 300);
00262
00263
00264 $this->_mail = true;
00265 $this->_rcpt = false;
00266 $this->_data = false;
00267 }
00268
00269
00277 public function rcpt($to)
00278 {
00279 if ($this->_mail !== true) {
00283 require_once 'Zend/Mail/Protocol/Exception.php';
00284 throw new Zend_Mail_Protocol_Exception('No sender reverse path has been supplied');
00285 }
00286
00287
00288 $this->_send('RCPT TO:<' . $to . '>');
00289 $this->_expect(array(250, 251), 300);
00290 $this->_rcpt = true;
00291 }
00292
00293
00301 public function data($data)
00302 {
00303
00304 if ($this->_rcpt !== true) {
00308 require_once 'Zend/Mail/Protocol/Exception.php';
00309 throw new Zend_Mail_Protocol_Exception('No recipient forward path has been supplied');
00310 }
00311
00312 $this->_send('DATA');
00313 $this->_expect(354, 120);
00314
00315 foreach (explode(Zend_Mime::LINEEND, $data) as $line) {
00316 if (strpos($line, '.') === 0) {
00317
00318 $line = '.' . $line;
00319 }
00320 $this->_send($line);
00321 }
00322
00323 $this->_send('.');
00324 $this->_expect(250, 600);
00325 $this->_data = true;
00326 }
00327
00328
00336 public function rset()
00337 {
00338 $this->_send('RSET');
00339
00340 $this->_expect(array(250, 220));
00341
00342 $this->_mail = false;
00343 $this->_rcpt = false;
00344 $this->_data = false;
00345 }
00346
00347
00355 public function noop()
00356 {
00357 $this->_send('NOOP');
00358 $this->_expect(250, 300);
00359 }
00360
00361
00370 public function vrfy($user)
00371 {
00372 $this->_send('VRFY ' . $user);
00373 $this->_expect(array(250, 251, 252), 300);
00374 }
00375
00376
00382 public function quit()
00383 {
00384 if ($this->_sess) {
00385 $this->_send('QUIT');
00386 $this->_expect(221, 300);
00387 $this->_stopSession();
00388 }
00389 }
00390
00391
00400 public function auth()
00401 {
00402 if ($this->_auth === true) {
00406 require_once 'Zend/Mail/Protocol/Exception.php';
00407 throw new Zend_Mail_Protocol_Exception('Already authenticated for this session');
00408 }
00409 }
00410
00411
00417 public function disconnect()
00418 {
00419 $this->_disconnect();
00420 }
00421
00422
00428 protected function _startSession()
00429 {
00430 $this->_sess = true;
00431 }
00432
00433
00439 protected function _stopSession()
00440 {
00441 $this->_sess = false;
00442 }
00443 }