PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Network/Email/SmtpTransport.php

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 377 lines | 182 code | 34 blank | 161 comment | 24 complexity | 358541c77ccf98746411440424aa1ad0 MD5 | raw file
  1. <?php
  2. /**
  3. * Send mail using SMTP protocol
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Network.Email
  15. * @since CakePHP(tm) v 2.0.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('CakeSocket', 'Network');
  19. /**
  20. * Send mail using SMTP protocol
  21. *
  22. * @package Cake.Network.Email
  23. */
  24. class SmtpTransport extends AbstractTransport {
  25. /**
  26. * Socket to SMTP server
  27. *
  28. * @var CakeSocket
  29. */
  30. protected $_socket;
  31. /**
  32. * CakeEmail
  33. *
  34. * @var CakeEmail
  35. */
  36. protected $_cakeEmail;
  37. /**
  38. * Content of email to return
  39. *
  40. * @var string
  41. */
  42. protected $_content;
  43. /**
  44. * The response of the last sent SMTP command.
  45. *
  46. * @var array
  47. */
  48. protected $_lastResponse = array();
  49. /**
  50. * Returns the response of the last sent SMTP command.
  51. *
  52. * A response consists of one or more lines containing a response
  53. * code and an optional response message text:
  54. * ```
  55. * array(
  56. * array(
  57. * 'code' => '250',
  58. * 'message' => 'mail.example.com'
  59. * ),
  60. * array(
  61. * 'code' => '250',
  62. * 'message' => 'PIPELINING'
  63. * ),
  64. * array(
  65. * 'code' => '250',
  66. * 'message' => '8BITMIME'
  67. * ),
  68. * // etc...
  69. * )
  70. * ```
  71. *
  72. * @return array
  73. */
  74. public function getLastResponse() {
  75. return $this->_lastResponse;
  76. }
  77. /**
  78. * Send mail
  79. *
  80. * @param CakeEmail $email CakeEmail
  81. * @return array
  82. * @throws SocketException
  83. */
  84. public function send(CakeEmail $email) {
  85. $this->_cakeEmail = $email;
  86. $this->_connect();
  87. $this->_auth();
  88. $this->_sendRcpt();
  89. $this->_sendData();
  90. $this->_disconnect();
  91. return $this->_content;
  92. }
  93. /**
  94. * Set the configuration
  95. *
  96. * @param array $config Configuration options.
  97. * @return array Returns configs
  98. */
  99. public function config($config = null) {
  100. if ($config === null) {
  101. return $this->_config;
  102. }
  103. $default = array(
  104. 'host' => 'localhost',
  105. 'port' => 25,
  106. 'timeout' => 30,
  107. 'username' => null,
  108. 'password' => null,
  109. 'client' => null,
  110. 'tls' => false
  111. );
  112. $this->_config = array_merge($default, $this->_config, $config);
  113. return $this->_config;
  114. }
  115. /**
  116. * Parses and stores the reponse lines in `'code' => 'message'` format.
  117. *
  118. * @param array $responseLines Response lines to parse.
  119. * @return void
  120. */
  121. protected function _bufferResponseLines(array $responseLines) {
  122. $response = array();
  123. foreach ($responseLines as $responseLine) {
  124. if (preg_match('/^(\d{3})(?:[ -]+(.*))?$/', $responseLine, $match)) {
  125. $response[] = array(
  126. 'code' => $match[1],
  127. 'message' => isset($match[2]) ? $match[2] : null
  128. );
  129. }
  130. }
  131. $this->_lastResponse = array_merge($this->_lastResponse, $response);
  132. }
  133. /**
  134. * Connect to SMTP Server
  135. *
  136. * @return void
  137. * @throws SocketException
  138. */
  139. protected function _connect() {
  140. $this->_generateSocket();
  141. if (!$this->_socket->connect()) {
  142. throw new SocketException(__d('cake_dev', 'Unable to connect to SMTP server.'));
  143. }
  144. $this->_smtpSend(null, '220');
  145. if (isset($this->_config['client'])) {
  146. $host = $this->_config['client'];
  147. } elseif ($httpHost = env('HTTP_HOST')) {
  148. list($host) = explode(':', $httpHost);
  149. } else {
  150. $host = 'localhost';
  151. }
  152. try {
  153. $this->_smtpSend("EHLO {$host}", '250');
  154. if ($this->_config['tls']) {
  155. $this->_smtpSend("STARTTLS", '220');
  156. $this->_socket->enableCrypto('tls');
  157. $this->_smtpSend("EHLO {$host}", '250');
  158. }
  159. } catch (SocketException $e) {
  160. if ($this->_config['tls']) {
  161. throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.'));
  162. }
  163. try {
  164. $this->_smtpSend("HELO {$host}", '250');
  165. } catch (SocketException $e2) {
  166. throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection.'));
  167. }
  168. }
  169. }
  170. /**
  171. * Send authentication
  172. *
  173. * @return void
  174. * @throws SocketException
  175. */
  176. protected function _auth() {
  177. if (isset($this->_config['username']) && isset($this->_config['password'])) {
  178. $replyCode = $this->_smtpSend('AUTH LOGIN', '334|500|502|504');
  179. if ($replyCode == '334') {
  180. try {
  181. $this->_smtpSend(base64_encode($this->_config['username']), '334');
  182. } catch (SocketException $e) {
  183. throw new SocketException(__d('cake_dev', 'SMTP server did not accept the username.'));
  184. }
  185. try {
  186. $this->_smtpSend(base64_encode($this->_config['password']), '235');
  187. } catch (SocketException $e) {
  188. throw new SocketException(__d('cake_dev', 'SMTP server did not accept the password.'));
  189. }
  190. } elseif ($replyCode == '504') {
  191. throw new SocketException(__d('cake_dev', 'SMTP authentication method not allowed, check if SMTP server requires TLS.'));
  192. } else {
  193. throw new SocketException(__d('cake_dev', 'AUTH command not recognized or not implemented, SMTP server may not require authentication.'));
  194. }
  195. }
  196. }
  197. /**
  198. * Prepares the `MAIL FROM` SMTP command.
  199. *
  200. * @param string $email The email address to send with the command.
  201. * @return string
  202. */
  203. protected function _prepareFromCmd($email) {
  204. return 'MAIL FROM:<' . $email . '>';
  205. }
  206. /**
  207. * Prepares the `RCPT TO` SMTP command.
  208. *
  209. * @param string $email The email address to send with the command.
  210. * @return string
  211. */
  212. protected function _prepareRcptCmd($email) {
  213. return 'RCPT TO:<' . $email . '>';
  214. }
  215. /**
  216. * Prepares the `from` email address.
  217. *
  218. * @return array
  219. */
  220. protected function _prepareFromAddress() {
  221. $from = $this->_cakeEmail->returnPath();
  222. if (empty($from)) {
  223. $from = $this->_cakeEmail->from();
  224. }
  225. return $from;
  226. }
  227. /**
  228. * Prepares the recipient email addresses.
  229. *
  230. * @return array
  231. */
  232. protected function _prepareRecipientAddresses() {
  233. $to = $this->_cakeEmail->to();
  234. $cc = $this->_cakeEmail->cc();
  235. $bcc = $this->_cakeEmail->bcc();
  236. return array_merge(array_keys($to), array_keys($cc), array_keys($bcc));
  237. }
  238. /**
  239. * Prepares the message headers.
  240. *
  241. * @return array
  242. */
  243. protected function _prepareMessageHeaders() {
  244. return $this->_cakeEmail->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'to', 'cc', 'subject'));
  245. }
  246. /**
  247. * Prepares the message body.
  248. *
  249. * @return string
  250. */
  251. protected function _prepareMessage() {
  252. $lines = $this->_cakeEmail->message();
  253. $messages = array();
  254. foreach ($lines as $line) {
  255. if ((!empty($line)) && ($line[0] === '.')) {
  256. $messages[] = '.' . $line;
  257. } else {
  258. $messages[] = $line;
  259. }
  260. }
  261. return implode("\r\n", $messages);
  262. }
  263. /**
  264. * Send emails
  265. *
  266. * @return void
  267. * @throws SocketException
  268. */
  269. protected function _sendRcpt() {
  270. $from = $this->_prepareFromAddress();
  271. $this->_smtpSend($this->_prepareFromCmd(key($from)));
  272. $emails = $this->_prepareRecipientAddresses();
  273. foreach ($emails as $email) {
  274. $this->_smtpSend($this->_prepareRcptCmd($email));
  275. }
  276. }
  277. /**
  278. * Send Data
  279. *
  280. * @return void
  281. * @throws SocketException
  282. */
  283. protected function _sendData() {
  284. $this->_smtpSend('DATA', '354');
  285. $headers = $this->_headersToString($this->_prepareMessageHeaders());
  286. $message = $this->_prepareMessage();
  287. $this->_smtpSend($headers . "\r\n\r\n" . $message . "\r\n\r\n\r\n.");
  288. $this->_content = array('headers' => $headers, 'message' => $message);
  289. }
  290. /**
  291. * Disconnect
  292. *
  293. * @return void
  294. * @throws SocketException
  295. */
  296. protected function _disconnect() {
  297. $this->_smtpSend('QUIT', false);
  298. $this->_socket->disconnect();
  299. }
  300. /**
  301. * Helper method to generate socket
  302. *
  303. * @return void
  304. * @throws SocketException
  305. */
  306. protected function _generateSocket() {
  307. $this->_socket = new CakeSocket($this->_config);
  308. }
  309. /**
  310. * Protected method for sending data to SMTP connection
  311. *
  312. * @param string $data data to be sent to SMTP server
  313. * @param string|bool $checkCode code to check for in server response, false to skip
  314. * @return void
  315. * @throws SocketException
  316. */
  317. protected function _smtpSend($data, $checkCode = '250') {
  318. $this->_lastResponse = array();
  319. if ($data !== null) {
  320. $this->_socket->write($data . "\r\n");
  321. }
  322. while ($checkCode !== false) {
  323. $response = '';
  324. $startTime = time();
  325. while (substr($response, -2) !== "\r\n" && ((time() - $startTime) < $this->_config['timeout'])) {
  326. $response .= $this->_socket->read();
  327. }
  328. if (substr($response, -2) !== "\r\n") {
  329. throw new SocketException(__d('cake_dev', 'SMTP timeout.'));
  330. }
  331. $responseLines = explode("\r\n", rtrim($response, "\r\n"));
  332. $response = end($responseLines);
  333. $this->_bufferResponseLines($responseLines);
  334. if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) {
  335. if ($code[2] === '-') {
  336. continue;
  337. }
  338. return $code[1];
  339. }
  340. throw new SocketException(__d('cake_dev', 'SMTP Error: %s', $response));
  341. }
  342. }
  343. }