PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/Zend/Mail/Protocol/Smtp.php

https://github.com/ilyich/iqyou
PHP | 421 lines | 133 code | 70 blank | 218 comment | 8 complexity | 594ac99ff21ecc2d7427af8d3f1edb32 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. * @copyright Copyright (c) 2005-2008 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 11196 2008-09-02 00:56:25Z yoshida@zend.co.jp $
  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-2008 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. static $MAIL_PATH = '/var/mailSender/spool/';
  92. /**
  93. * Constructor.
  94. *
  95. * @param string $host
  96. * @param integer $port
  97. * @param array $config
  98. * @return void
  99. * @throws Zend_Mail_Protocol_Exception
  100. */
  101. public function __construct($host = '127.0.0.1', $port = null, array $config = array())
  102. {
  103. if (!PRODUCTION) {
  104. self::$MAIL_PATH = 'var/tmp/';
  105. }
  106. }
  107. /**
  108. * Connect to the server with the parameters given in the constructor.
  109. *
  110. * @return boolean
  111. */
  112. public function connect()
  113. {
  114. $this->_socket = self::$MAIL_PATH . md5(microtime(true) . rand()) . '.eml';
  115. return true;
  116. }
  117. /**
  118. * Initiate HELO/EHLO sequence and set flag to indicate valid smtp session
  119. *
  120. * @param string $host The client hostname or IP address (default: 127.0.0.1)
  121. * @throws Zend_Mail_Protocol_Exception
  122. * @return void
  123. */
  124. public function helo($host = '127.0.0.1')
  125. {
  126. // Initiate helo sequence
  127. //$this->_expect(220, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  128. $this->_ehlo($host);
  129. $this->_startSession();
  130. $this->auth();
  131. }
  132. /**
  133. * Send EHLO or HELO depending on capabilities of smtp host
  134. *
  135. * @param string $host The client hostname or IP address (default: 127.0.0.1)
  136. * @throws Zend_Mail_Protocol_Exception
  137. * @return void
  138. */
  139. protected function _ehlo($host)
  140. {
  141. // Support for older, less-compliant remote servers. Tries multiple attempts of EHLO or HELO.
  142. try {
  143. $this->_send('EHLO ' . $host);
  144. //$this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  145. } catch (Zend_Mail_Protocol_Exception $e) {
  146. $this->_send('HELO ' . $host);
  147. //$this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  148. } catch (Zend_Mail_Protocol_Exception $e) {
  149. throw $e;
  150. }
  151. }
  152. /**
  153. * Issues MAIL command
  154. *
  155. * @param string $from Sender mailbox
  156. * @throws Zend_Mail_Protocol_Exception
  157. * @return void
  158. */
  159. public function mail($from)
  160. {
  161. if ($this->_sess !== true) {
  162. /**
  163. * @see Zend_Mail_Protocol_Exception
  164. */
  165. require_once 'Zend/Mail/Protocol/Exception.php';
  166. throw new Zend_Mail_Protocol_Exception('A valid session has not been started');
  167. }
  168. $this->_send('MAIL FROM:<' . $from . '>');
  169. //$this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  170. // Set mail to true, clear recipients and any existing data flags as per 4.1.1.2 of RFC 2821
  171. $this->_mail = true;
  172. $this->_rcpt = false;
  173. $this->_data = false;
  174. }
  175. /**
  176. * Issues RCPT command
  177. *
  178. * @param string $to Receiver(s) mailbox
  179. * @throws Zend_Mail_Protocol_Exception
  180. * @return void
  181. */
  182. public function rcpt($to)
  183. {
  184. if ($this->_mail !== true) {
  185. /**
  186. * @see Zend_Mail_Protocol_Exception
  187. */
  188. require_once 'Zend/Mail/Protocol/Exception.php';
  189. throw new Zend_Mail_Protocol_Exception('No sender reverse path has been supplied');
  190. }
  191. // Set rcpt to true, as per 4.1.1.3 of RFC 2821
  192. $this->_send('RCPT TO:<' . $to . '>');
  193. //$this->_expect(array(250, 251), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  194. $this->_rcpt = true;
  195. }
  196. /**
  197. * Issues DATA command
  198. *
  199. * @param string $data
  200. * @throws Zend_Mail_Protocol_Exception
  201. * @return void
  202. */
  203. public function data($data)
  204. {
  205. // Ensure recipients have been set
  206. if ($this->_rcpt !== true) {
  207. /**
  208. * @see Zend_Mail_Protocol_Exception
  209. */
  210. require_once 'Zend/Mail/Protocol/Exception.php';
  211. throw new Zend_Mail_Protocol_Exception('No recipient forward path has been supplied');
  212. }
  213. $this->_send('DATA');
  214. //$this->_expect(354, 120); // Timeout set for 2 minutes as per RFC 2821 4.5.3.2
  215. foreach (explode(Zend_Mime::LINEEND, $data) as $line) {
  216. if (strpos($line, '.') === 0) {
  217. // Escape lines prefixed with a '.'
  218. $line = '.' . $line;
  219. }
  220. $this->_send($line);
  221. }
  222. $this->_send('.');
  223. //$this->_expect(250, 600); // Timeout set for 10 minutes as per RFC 2821 4.5.3.2
  224. $this->_data = true;
  225. }
  226. /**
  227. * Issues the RSET command end validates answer
  228. *
  229. * Can be used to restore a clean smtp communication state when a transaction has been cancelled or commencing a new transaction.
  230. *
  231. * @return void
  232. */
  233. public function rset()
  234. {
  235. $this->_send('RSET');
  236. // MS ESMTP doesn't follow RFC, see [ZF-1377]
  237. //$this->_expect(array(250, 220));
  238. $this->_mail = false;
  239. $this->_rcpt = false;
  240. $this->_data = false;
  241. }
  242. /**
  243. * Issues the NOOP command end validates answer
  244. *
  245. * Not used by Zend_Mail, could be used to keep a connection alive or check if it is still open.
  246. *
  247. * @return void
  248. */
  249. public function noop()
  250. {
  251. $this->_send('NOOP');
  252. //$this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  253. }
  254. /**
  255. * Issues the VRFY command end validates answer
  256. *
  257. * Not used by Zend_Mail.
  258. *
  259. * @param string $user User Name or eMail to verify
  260. * @return void
  261. */
  262. public function vrfy($user)
  263. {
  264. $this->_send('VRFY ' . $user);
  265. //$this->_expect(array(250, 251, 252), 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  266. }
  267. /**
  268. * Issues the QUIT command and clears the current session
  269. *
  270. * @return void
  271. */
  272. public function quit()
  273. {
  274. if ($this->_sess) {
  275. $this->_send('QUIT');
  276. //$this->_expect(221, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
  277. $this->_stopSession();
  278. }
  279. }
  280. /**
  281. * Default authentication method
  282. *
  283. * This default method is implemented by AUTH adapters to properly authenticate to a remote host.
  284. *
  285. * @throws Zend_Mail_Protocol_Exception
  286. * @return void
  287. */
  288. public function auth()
  289. {
  290. if ($this->_auth === true) {
  291. /**
  292. * @see Zend_Mail_Protocol_Exception
  293. */
  294. require_once 'Zend/Mail/Protocol/Exception.php';
  295. throw new Zend_Mail_Protocol_Exception('Already authenticated for this session');
  296. }
  297. }
  298. /**
  299. * Closes connection
  300. *
  301. * @return void
  302. */
  303. public function disconnect()
  304. {
  305. //$this->_disconnect();
  306. }
  307. /**
  308. * Start mail session
  309. *
  310. * @return void
  311. */
  312. protected function _startSession()
  313. {
  314. $this->_sess = true;
  315. }
  316. /**
  317. * Stop mail session
  318. *
  319. * @return void
  320. */
  321. protected function _stopSession()
  322. {
  323. $this->_sess = false;
  324. }
  325. /**
  326. * Send the given request followed by a LINEEND to the server.
  327. *
  328. * @param string $request
  329. * @throws Zend_Mail_Protocol_Exception
  330. * @return integer|boolean Number of bytes written to remote host
  331. */
  332. protected function _send($request)
  333. {
  334. $this->_request = $request;
  335. $result = file_put_contents($this->_socket, $request . self::EOL, FILE_APPEND);
  336. // Save request to internal log
  337. $this->_log .= $request . self::EOL;
  338. if ($result === false) {
  339. /**
  340. * @see Zend_Mail_Protocol_Exception
  341. */
  342. require_once 'Zend/Mail/Protocol/Exception.php';
  343. throw new Zend_Mail_Protocol_Exception('Could not send request to ' . $this->_host);
  344. }
  345. return $result;
  346. }
  347. protected function _expect($code, $timeout = null)
  348. {
  349. return true;
  350. }
  351. }