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

/tine20/library/Zend/Mail/Protocol/Smtp.php

https://gitlab.com/israel.correa/Expresso
PHP | 443 lines | 166 code | 66 blank | 211 comment | 19 complexity | 6474a7bc3971ca81394476e307c3cc78 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, LGPL-2.1, LGPL-3.0, JSON, Apache-2.0
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Smtp.php 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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-2009 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. if (!stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
  173. /**
  174. * @see Zend_Mail_Protocol_Exception
  175. */
  176. require_once 'Zend/Mail/Protocol/Exception.php';
  177. throw new Zend_Mail_Protocol_Exception('Unable to connect via TLS');
  178. }
  179. $this->_ehlo($host);
  180. }
  181. $this->_startSession();
  182. $this->auth();
  183. }
  184. /**
  185. * Send EHLO or HELO depending on capabilities of smtp host
  186. *
  187. * @param string $host The client hostname or IP address (default: 127.0.0.1)
  188. * @throws Zend_Mail_Protocol_Exception
  189. * @return void
  190. */
  191. protected function _ehlo($host)
  192. {
  193. // Support for older, less-compliant remote servers. Tries multiple attempts of EHLO or HELO.
  194. try {
  195. $this->_send('EHLO ' . $host);
  196. $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  197. } catch (Zend_Mail_Protocol_Exception $e) {
  198. $this->_send('HELO ' . $host);
  199. $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  200. } catch (Zend_Mail_Protocol_Exception $e) {
  201. throw $e;
  202. }
  203. }
  204. /**
  205. * Issues MAIL command
  206. *
  207. * @param string $from Sender mailbox
  208. * @throws Zend_Mail_Protocol_Exception
  209. * @return void
  210. */
  211. public function mail($from)
  212. {
  213. if ($this->_sess !== true) {
  214. /**
  215. * @see Zend_Mail_Protocol_Exception
  216. */
  217. require_once 'Zend/Mail/Protocol/Exception.php';
  218. throw new Zend_Mail_Protocol_Exception('A valid session has not been started');
  219. }
  220. $this->_send('MAIL FROM:<' . $from . '>');
  221. $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  222. // Set mail to true, clear recipients and any existing data flags as per 4.1.1.2 of RFC 2821
  223. $this->_mail = true;
  224. $this->_rcpt = false;
  225. $this->_data = false;
  226. }
  227. /**
  228. * Issues RCPT command
  229. *
  230. * @param string $to Receiver(s) mailbox
  231. * @throws Zend_Mail_Protocol_Exception
  232. * @return void
  233. */
  234. public function rcpt($to)
  235. {
  236. if ($this->_mail !== true) {
  237. /**
  238. * @see Zend_Mail_Protocol_Exception
  239. */
  240. require_once 'Zend/Mail/Protocol/Exception.php';
  241. throw new Zend_Mail_Protocol_Exception('No sender reverse path has been supplied');
  242. }
  243. // Set rcpt to true, as per 4.1.1.3 of RFC 2821
  244. $this->_send('RCPT TO:<' . $to . '>');
  245. $this->_expect(array(250, 251), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  246. $this->_rcpt = true;
  247. }
  248. /**
  249. * Issues DATA command
  250. *
  251. * @param string $data
  252. * @throws Zend_Mail_Protocol_Exception
  253. * @return void
  254. */
  255. public function data($data)
  256. {
  257. // Ensure recipients have been set
  258. if ($this->_rcpt !== true) {
  259. /**
  260. * @see Zend_Mail_Protocol_Exception
  261. */
  262. require_once 'Zend/Mail/Protocol/Exception.php';
  263. throw new Zend_Mail_Protocol_Exception('No recipient forward path has been supplied');
  264. }
  265. $this->_send('DATA');
  266. $this->_expect(354, 120); // Timeout set for 2 minutes as per RFC 2821 4.5.3.2
  267. foreach (explode(Zend_Mime::LINEEND, $data) as $line) {
  268. if (strpos($line, '.') === 0) {
  269. // Escape lines prefixed with a '.'
  270. $line = '.' . $line;
  271. }
  272. $this->_send($line);
  273. }
  274. $this->_send('.');
  275. $this->_expect(250, 600); // Timeout set for 10 minutes as per RFC 2821 4.5.3.2
  276. $this->_data = true;
  277. }
  278. /**
  279. * Issues the RSET command end validates answer
  280. *
  281. * Can be used to restore a clean smtp communication state when a transaction has been cancelled or commencing a new transaction.
  282. *
  283. * @return void
  284. */
  285. public function rset()
  286. {
  287. $this->_send('RSET');
  288. // MS ESMTP doesn't follow RFC, see [ZF-1377]
  289. $this->_expect(array(250, 220));
  290. $this->_mail = false;
  291. $this->_rcpt = false;
  292. $this->_data = false;
  293. }
  294. /**
  295. * Issues the NOOP command end validates answer
  296. *
  297. * Not used by Zend_Mail, could be used to keep a connection alive or check if it is still open.
  298. *
  299. * @return void
  300. */
  301. public function noop()
  302. {
  303. $this->_send('NOOP');
  304. $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  305. }
  306. /**
  307. * Issues the VRFY command end validates answer
  308. *
  309. * Not used by Zend_Mail.
  310. *
  311. * @param string $user User Name or eMail to verify
  312. * @return void
  313. */
  314. public function vrfy($user)
  315. {
  316. $this->_send('VRFY ' . $user);
  317. $this->_expect(array(250, 251, 252), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  318. }
  319. /**
  320. * Issues the QUIT command and clears the current session
  321. *
  322. * @return void
  323. */
  324. public function quit()
  325. {
  326. if ($this->_sess) {
  327. $this->_send('QUIT');
  328. $this->_expect(221, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  329. $this->_stopSession();
  330. }
  331. }
  332. /**
  333. * Default authentication method
  334. *
  335. * This default method is implemented by AUTH adapters to properly authenticate to a remote host.
  336. *
  337. * @throws Zend_Mail_Protocol_Exception
  338. * @return void
  339. */
  340. public function auth()
  341. {
  342. if ($this->_auth === true) {
  343. /**
  344. * @see Zend_Mail_Protocol_Exception
  345. */
  346. require_once 'Zend/Mail/Protocol/Exception.php';
  347. throw new Zend_Mail_Protocol_Exception('Already authenticated for this session');
  348. }
  349. }
  350. /**
  351. * Closes connection
  352. *
  353. * @return void
  354. */
  355. public function disconnect()
  356. {
  357. $this->_disconnect();
  358. }
  359. /**
  360. * Start mail session
  361. *
  362. * @return void
  363. */
  364. protected function _startSession()
  365. {
  366. $this->_sess = true;
  367. }
  368. /**
  369. * Stop mail session
  370. *
  371. * @return void
  372. */
  373. protected function _stopSession()
  374. {
  375. $this->_sess = false;
  376. }
  377. }