Smtp.php 12 KB

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