PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/tags/release-1.0.4/library/Zend/Mail/Protocol/Smtp.php

https://github.com/bhaumik25/zend-framework
PHP | 419 lines | 166 code | 66 blank | 187 comment | 19 complexity | 9cff25e401f0fffa1849959dfe40be41 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage Protocol
  18. * @version $Id$
  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. */
  22. /**
  23. * Zend_Mime
  24. */
  25. require_once 'Zend/Mime.php';
  26. /**
  27. * Zend_Mail_Protocol_Abstract
  28. */
  29. require_once 'Zend/Mail/Protocol/Abstract.php';
  30. /**
  31. * Smtp implementation of Zend_Mail_Protocol_Abstract
  32. *
  33. * Minimum implementation according to RFC2821: EHLO, MAIL FROM, RCPT TO, DATA, RSET, NOOP, QUIT
  34. *
  35. * @category Zend
  36. * @package Zend_Mail
  37. * @subpackage Protocol
  38. * @throws Zend_Mail_Protocol_Exception
  39. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Mail_Protocol_Smtp extends Zend_Mail_Protocol_Abstract
  43. {
  44. /**
  45. * The transport method for the socket
  46. *
  47. * @var string
  48. */
  49. protected $_transport = 'tcp';
  50. /**
  51. * Indicates that a session is requested to be secure
  52. *
  53. * @var string
  54. */
  55. protected $_secure;
  56. /**
  57. * Indicates an smtp session has been started by the HELO command
  58. *
  59. * @var boolean
  60. */
  61. protected $_sess = false;
  62. /**
  63. * Indicates the HELO command has been issues
  64. *
  65. * @var unknown_type
  66. */
  67. protected $_helo = false;
  68. /**
  69. * Indicates an smtp AUTH has been issued and authenticated
  70. *
  71. * @var unknown_type
  72. */
  73. protected $_auth = false;
  74. /**
  75. * Indicates a MAIL command has been issued
  76. *
  77. * @var unknown_type
  78. */
  79. protected $_mail = false;
  80. /**
  81. * Indicates one or more RCTP commands have been issued
  82. *
  83. * @var unknown_type
  84. */
  85. protected $_rcpt = false;
  86. /**
  87. * Indicates that DATA has been issued and sent
  88. *
  89. * @var unknown_type
  90. */
  91. protected $_data = null;
  92. /**
  93. * Constructor.
  94. *
  95. * @param string $host
  96. * @param integer $port
  97. * @param array $config
  98. * @return void
  99. */
  100. public function __construct($host = '127.0.0.1', $port = null, array $config = array())
  101. {
  102. if (isset($config['ssl'])) {
  103. switch (strtolower($config['ssl'])) {
  104. case 'tls':
  105. $this->_secure = 'tls';
  106. break;
  107. case 'ssl':
  108. $this->_transport = 'ssl';
  109. $this->_secure = 'ssl';
  110. if ($port == null) {
  111. $port = 465;
  112. }
  113. break;
  114. default:
  115. require_once 'Zend/Mail/Protocol/Exception.php';
  116. throw new Zend_Mail_Protocol_Exception($ssl . ' is unsupported SSL type');
  117. break;
  118. }
  119. }
  120. // If no port has been specified then check the master PHP ini file. Defaults to 25 if the ini setting is null.
  121. if ($port == null) {
  122. if (($port = ini_get('smtp_port')) == '') {
  123. $port = 25;
  124. }
  125. }
  126. parent::__construct($host, $port);
  127. }
  128. /**
  129. * Connect to the server with the parameters given in the constructor.
  130. *
  131. * @return boolean
  132. */
  133. public function connect()
  134. {
  135. return $this->_connect($this->_transport . '://' . $this->_host . ':'. $this->_port);
  136. }
  137. /**
  138. * Initiate HELO/EHLO sequence and set flag to indicate valid smtp session
  139. *
  140. * @param string $host The client hostname or IP address (default: 127.0.0.1)
  141. * @throws Zend_Mail_Protocol_Exception
  142. * @return void
  143. */
  144. public function helo($host = '127.0.0.1')
  145. {
  146. // Respect RFC 2821 and disallow HELO attempts if session is already initiated.
  147. if ($this->_sess === true) {
  148. require_once 'Zend/Mail/Protocol/Exception.php';
  149. throw new Zend_Mail_Protocol_Exception('Cannot issue HELO to existing session');
  150. }
  151. // Validate client hostname
  152. if (!$this->_validHost->isValid($host)) {
  153. require_once 'Zend/Mail/Protocol/Exception.php';
  154. throw new Zend_Mail_Protocol_Exception(join(', ', $this->_validHost->getMessage()));
  155. }
  156. // Initiate helo sequence
  157. $this->_expect(220, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  158. $this->_ehlo($host);
  159. // If a TLS session is required, commence negotiation
  160. if ($this->_secure == 'tls') {
  161. $this->_send('STARTTLS');
  162. $this->_expect(220, 180);
  163. if (!stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
  164. require_once 'Zend/Mail/Protocol/Exception.php';
  165. throw new Zend_Mail_Protocol_Exception('Unable to connect via TLS');
  166. }
  167. $this->_ehlo($host);
  168. }
  169. $this->_startSession();
  170. $this->auth();
  171. }
  172. /**
  173. * Send EHLO or HELO depending on capabilities of smtp host
  174. *
  175. * @param string $host The client hostname or IP address (default: 127.0.0.1)
  176. * @throws Zend_Mail_Protocol_Exception
  177. * @return void
  178. */
  179. protected function _ehlo($host)
  180. {
  181. // Support for older, less-compliant remote servers. Tries multiple attempts of EHLO or HELO.
  182. try {
  183. $this->_send('EHLO ' . $host);
  184. $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  185. } catch (Zend_Mail_Protocol_Exception $e) {
  186. $this->_send('HELO ' . $host);
  187. $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  188. } catch (Zend_Mail_Protocol_Exception $e) {
  189. throw $e;
  190. }
  191. }
  192. /**
  193. * Issues MAIL command
  194. *
  195. * @param string $from Sender mailbox
  196. * @throws Zend_Mail_Protocol_Exception
  197. * @return void
  198. */
  199. public function mail($from)
  200. {
  201. if ($this->_sess !== true) {
  202. require_once 'Zend/Mail/Protocol/Exception.php';
  203. throw new Zend_Mail_Protocol_Exception('A valid session has not been started');
  204. }
  205. $this->_send('MAIL FROM:<' . $from . '>');
  206. $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  207. // Set mail to true, clear recipients and any existing data flags as per 4.1.1.2 of RFC 2821
  208. $this->_mail = true;
  209. $this->_rcpt = false;
  210. $this->_data = false;
  211. }
  212. /**
  213. * Issues RCPT command
  214. *
  215. * @param string $to Receiver(s) mailbox
  216. * @throws Zend_Mail_Protocol_Exception
  217. * @return void
  218. */
  219. public function rcpt($to)
  220. {
  221. if ($this->_mail !== true) {
  222. require_once 'Zend/Mail/Protocol/Exception.php';
  223. throw new Zend_Mail_Protocol_Exception('No sender reverse path has been supplied');
  224. }
  225. // Set rcpt to true, as per 4.1.1.3 of RFC 2821
  226. $this->_send('RCPT TO:<' . $to . '>');
  227. $this->_expect(array(250, 251), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  228. $this->_rcpt = true;
  229. }
  230. /**
  231. * Issues DATA command
  232. *
  233. * @param string $data
  234. * @throws Zend_Mail_Protocol_Exception
  235. * @return void
  236. */
  237. public function data($data)
  238. {
  239. // Ensure recipients have been set
  240. if ($this->_rcpt !== true) {
  241. require_once 'Zend/Mail/Protocol/Exception.php';
  242. throw new Zend_Mail_Protocol_Exception('No recipient forward path has been supplied');
  243. }
  244. $this->_send('DATA');
  245. $this->_expect(354, 120); // Timeout set for 2 minutes as per RFC 2821 4.5.3.2
  246. foreach (explode(Zend_Mime::LINEEND, $data) as $line) {
  247. if (strpos($line, '.') === 0) {
  248. // Escape lines prefixed with a '.'
  249. $line = '.' . $line;
  250. }
  251. $this->_send($line);
  252. }
  253. $this->_send('.');
  254. $this->_expect(250, 600); // Timeout set for 10 minutes as per RFC 2821 4.5.3.2
  255. $this->_data = true;
  256. }
  257. /**
  258. * Issues the RSET command end validates answer
  259. *
  260. * Can be used to restore a clean smtp communication state when a transaction has been cancelled or commencing a new transaction.
  261. *
  262. * @return void
  263. */
  264. public function rset()
  265. {
  266. $this->_send('RSET');
  267. // MS ESMTP doesn't follow RFC, see [ZF-1377]
  268. $this->_expect(array(250, 220));
  269. $this->_mail = false;
  270. $this->_rcpt = false;
  271. $this->_data = false;
  272. }
  273. /**
  274. * Issues the NOOP command end validates answer
  275. *
  276. * Not used by Zend_Mail, could be used to keep a connection alive or check if it is still open.
  277. *
  278. * @return void
  279. */
  280. public function noop()
  281. {
  282. $this->_send('NOOP');
  283. $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  284. }
  285. /**
  286. * Issues the VRFY command end validates answer
  287. *
  288. * Not used by Zend_Mail.
  289. *
  290. * @param string $user User Name or eMail to verify
  291. * @return void
  292. */
  293. public function vrfy($user)
  294. {
  295. $this->_send('VRFY ' . $user);
  296. $this->_expect(array(250, 251, 252), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  297. }
  298. /**
  299. * Issues the QUIT command and clears the current session
  300. *
  301. * @return void
  302. */
  303. public function quit()
  304. {
  305. if ($this->_sess) {
  306. $this->_send('QUIT');
  307. $this->_expect(221, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  308. $this->_stopSession();
  309. }
  310. }
  311. /**
  312. * Default authentication method
  313. *
  314. * This default method is implemented by AUTH adapters to properly authenticate to a remote host.
  315. *
  316. * @throws Zend_Mail_Protocol_Exception
  317. * @return void
  318. */
  319. public function auth()
  320. {
  321. if ($this->_auth === true) {
  322. require_once 'Zend/Mail/Protocol/Exception.php';
  323. throw new Zend_Mail_Protocol_Exception('Already authenticated for this session');
  324. }
  325. }
  326. /**
  327. * Closes connection
  328. *
  329. * @return void
  330. */
  331. public function disconnect()
  332. {
  333. $this->_disconnect();
  334. }
  335. /**
  336. * Start mail session
  337. *
  338. * @return void
  339. */
  340. protected function _startSession()
  341. {
  342. $this->_sess = true;
  343. }
  344. /**
  345. * Stop mail session
  346. *
  347. * @return void
  348. */
  349. protected function _stopSession()
  350. {
  351. $this->_sess = false;
  352. }
  353. }