2011-03-01 16:47:05 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Send mail using SMTP protocol
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2011-03-01 16:47:05 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-03-01 16:47:05 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Network.Email
|
2011-03-01 16:47:05 +00:00
|
|
|
* @since CakePHP(tm) v 2.0.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
2016-06-03 02:04:50 +00:00
|
|
|
App::uses('AbstractTransport', 'Network/Email');
|
2011-04-13 03:29:34 +00:00
|
|
|
App::uses('CakeSocket', 'Network');
|
2011-03-01 16:47:05 +00:00
|
|
|
|
|
|
|
/**
|
2011-12-08 15:35:02 +00:00
|
|
|
* Send mail using SMTP protocol
|
2011-03-01 16:47:05 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Network.Email
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
|
|
|
class SmtpTransport extends AbstractTransport {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket to SMTP server
|
|
|
|
*
|
2011-07-29 03:56:10 +00:00
|
|
|
* @var CakeSocket
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
|
|
|
protected $_socket;
|
|
|
|
|
2011-08-26 00:31:18 +00:00
|
|
|
/**
|
|
|
|
* Content of email to return
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_content;
|
|
|
|
|
2014-03-13 15:28:54 +00:00
|
|
|
/**
|
|
|
|
* The response of the last sent SMTP command.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_lastResponse = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the response of the last sent SMTP command.
|
|
|
|
*
|
|
|
|
* A response consists of one or more lines containing a response
|
|
|
|
* code and an optional response message text:
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2014-03-13 15:28:54 +00:00
|
|
|
* array(
|
|
|
|
* array(
|
|
|
|
* 'code' => '250',
|
|
|
|
* 'message' => 'mail.example.com'
|
|
|
|
* ),
|
|
|
|
* array(
|
|
|
|
* 'code' => '250',
|
|
|
|
* 'message' => 'PIPELINING'
|
|
|
|
* ),
|
|
|
|
* array(
|
|
|
|
* 'code' => '250',
|
|
|
|
* 'message' => '8BITMIME'
|
|
|
|
* ),
|
|
|
|
* // etc...
|
|
|
|
* )
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2014-03-13 15:28:54 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getLastResponse() {
|
|
|
|
return $this->_lastResponse;
|
|
|
|
}
|
|
|
|
|
2011-03-01 16:47:05 +00:00
|
|
|
/**
|
|
|
|
* Send mail
|
|
|
|
*
|
2011-07-29 03:56:10 +00:00
|
|
|
* @param CakeEmail $email CakeEmail
|
2011-08-26 00:31:18 +00:00
|
|
|
* @return array
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
|
|
|
public function send(CakeEmail $email) {
|
|
|
|
$this->_connect();
|
|
|
|
$this->_auth();
|
2016-08-01 18:09:45 +00:00
|
|
|
$this->_sendRcpt($email);
|
|
|
|
$this->_sendData($email);
|
2011-03-01 16:47:05 +00:00
|
|
|
$this->_disconnect();
|
|
|
|
|
2011-08-26 00:31:18 +00:00
|
|
|
return $this->_content;
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
|
|
|
|
2011-04-13 04:04:37 +00:00
|
|
|
/**
|
|
|
|
* Set the configuration
|
|
|
|
*
|
2014-06-04 18:58:55 +00:00
|
|
|
* @param array $config Configuration options.
|
2013-05-22 06:02:27 +00:00
|
|
|
* @return array Returns configs
|
2011-04-13 04:04:37 +00:00
|
|
|
*/
|
2013-05-22 06:02:27 +00:00
|
|
|
public function config($config = null) {
|
|
|
|
if ($config === null) {
|
|
|
|
return $this->_config;
|
|
|
|
}
|
2011-04-13 04:04:37 +00:00
|
|
|
$default = array(
|
|
|
|
'host' => 'localhost',
|
|
|
|
'port' => 25,
|
|
|
|
'timeout' => 30,
|
|
|
|
'username' => null,
|
|
|
|
'password' => null,
|
2012-07-21 19:28:38 +00:00
|
|
|
'client' => null,
|
2015-10-01 19:47:30 +00:00
|
|
|
'tls' => false,
|
2015-10-06 08:10:34 +00:00
|
|
|
'ssl_allow_self_signed' => false
|
2011-04-13 04:04:37 +00:00
|
|
|
);
|
2013-08-12 01:28:17 +00:00
|
|
|
$this->_config = array_merge($default, $this->_config, $config);
|
2013-05-22 06:02:27 +00:00
|
|
|
return $this->_config;
|
2011-04-13 04:04:37 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 15:28:54 +00:00
|
|
|
/**
|
|
|
|
* Parses and stores the reponse lines in `'code' => 'message'` format.
|
|
|
|
*
|
2014-06-04 18:58:55 +00:00
|
|
|
* @param array $responseLines Response lines to parse.
|
2014-03-13 15:28:54 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _bufferResponseLines(array $responseLines) {
|
|
|
|
$response = array();
|
|
|
|
foreach ($responseLines as $responseLine) {
|
2014-03-15 10:47:13 +00:00
|
|
|
if (preg_match('/^(\d{3})(?:[ -]+(.*))?$/', $responseLine, $match)) {
|
2014-03-13 15:28:54 +00:00
|
|
|
$response[] = array(
|
|
|
|
'code' => $match[1],
|
|
|
|
'message' => isset($match[2]) ? $match[2] : null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->_lastResponse = array_merge($this->_lastResponse, $response);
|
|
|
|
}
|
|
|
|
|
2011-03-01 16:47:05 +00:00
|
|
|
/**
|
|
|
|
* Connect to SMTP Server
|
|
|
|
*
|
|
|
|
* @return void
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
|
|
|
protected function _connect() {
|
|
|
|
$this->_generateSocket();
|
|
|
|
if (!$this->_socket->connect()) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'Unable to connect to SMTP server.'));
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
|
|
|
$this->_smtpSend(null, '220');
|
|
|
|
|
|
|
|
if (isset($this->_config['client'])) {
|
|
|
|
$host = $this->_config['client'];
|
|
|
|
} elseif ($httpHost = env('HTTP_HOST')) {
|
|
|
|
list($host) = explode(':', $httpHost);
|
|
|
|
} else {
|
|
|
|
$host = 'localhost';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->_smtpSend("EHLO {$host}", '250');
|
2012-07-21 19:28:38 +00:00
|
|
|
if ($this->_config['tls']) {
|
|
|
|
$this->_smtpSend("STARTTLS", '220');
|
|
|
|
$this->_socket->enableCrypto('tls');
|
|
|
|
$this->_smtpSend("EHLO {$host}", '250');
|
|
|
|
}
|
2011-03-01 16:47:05 +00:00
|
|
|
} catch (SocketException $e) {
|
2012-07-21 19:28:38 +00:00
|
|
|
if ($this->_config['tls']) {
|
|
|
|
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.'));
|
|
|
|
}
|
2011-03-01 16:47:05 +00:00
|
|
|
try {
|
|
|
|
$this->_smtpSend("HELO {$host}", '250');
|
|
|
|
} catch (SocketException $e2) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection.'));
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send authentication
|
|
|
|
*
|
|
|
|
* @return void
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
|
|
|
protected function _auth() {
|
|
|
|
if (isset($this->_config['username']) && isset($this->_config['password'])) {
|
2014-07-18 12:53:22 +00:00
|
|
|
$replyCode = $this->_smtpSend('AUTH LOGIN', '334|500|502|504');
|
|
|
|
if ($replyCode == '334') {
|
|
|
|
try {
|
|
|
|
$this->_smtpSend(base64_encode($this->_config['username']), '334');
|
|
|
|
} catch (SocketException $e) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the username.'));
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
2014-07-18 12:53:22 +00:00
|
|
|
try {
|
|
|
|
$this->_smtpSend(base64_encode($this->_config['password']), '235');
|
|
|
|
} catch (SocketException $e) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the password.'));
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
2014-07-18 12:53:22 +00:00
|
|
|
} elseif ($replyCode == '504') {
|
|
|
|
throw new SocketException(__d('cake_dev', 'SMTP authentication method not allowed, check if SMTP server requires TLS.'));
|
|
|
|
} else {
|
|
|
|
throw new SocketException(__d('cake_dev', 'AUTH command not recognized or not implemented, SMTP server may not require authentication.'));
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-13 15:06:43 +00:00
|
|
|
* Prepares the `MAIL FROM` SMTP command.
|
2011-03-01 16:47:05 +00:00
|
|
|
*
|
2014-03-13 15:06:43 +00:00
|
|
|
* @param string $email The email address to send with the command.
|
|
|
|
* @return string
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
2014-03-13 15:06:43 +00:00
|
|
|
protected function _prepareFromCmd($email) {
|
|
|
|
return 'MAIL FROM:<' . $email . '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the `RCPT TO` SMTP command.
|
|
|
|
*
|
|
|
|
* @param string $email The email address to send with the command.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _prepareRcptCmd($email) {
|
|
|
|
return 'RCPT TO:<' . $email . '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the `from` email address.
|
|
|
|
*
|
2016-08-01 21:46:25 +00:00
|
|
|
* @param CakeEmail $email CakeEmail
|
2014-03-13 15:06:43 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2016-08-02 07:30:43 +00:00
|
|
|
protected function _prepareFromAddress(CakeEmail $email) {
|
2016-08-01 18:09:45 +00:00
|
|
|
$from = $email->returnPath();
|
2013-06-12 09:31:49 +00:00
|
|
|
if (empty($from)) {
|
2016-08-01 18:09:45 +00:00
|
|
|
$from = $email->from();
|
2013-06-12 09:31:49 +00:00
|
|
|
}
|
2014-03-13 15:06:43 +00:00
|
|
|
return $from;
|
|
|
|
}
|
2011-03-01 16:47:05 +00:00
|
|
|
|
2014-03-13 15:06:43 +00:00
|
|
|
/**
|
|
|
|
* Prepares the recipient email addresses.
|
|
|
|
*
|
2016-08-01 21:46:25 +00:00
|
|
|
* @param CakeEmail $email CakeEmail
|
2014-03-13 15:06:43 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2016-08-01 21:46:25 +00:00
|
|
|
protected function _prepareRecipientAddresses(CakeEmail $email) {
|
2016-08-01 18:09:45 +00:00
|
|
|
$to = $email->to();
|
|
|
|
$cc = $email->cc();
|
|
|
|
$bcc = $email->bcc();
|
2014-03-13 15:06:43 +00:00
|
|
|
return array_merge(array_keys($to), array_keys($cc), array_keys($bcc));
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-13 15:06:43 +00:00
|
|
|
* Prepares the message headers.
|
2011-03-01 16:47:05 +00:00
|
|
|
*
|
2016-08-01 21:46:25 +00:00
|
|
|
* @param CakeEmail $email CakeEmail
|
2014-03-13 15:06:43 +00:00
|
|
|
* @return array
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
2016-08-01 21:46:25 +00:00
|
|
|
protected function _prepareMessageHeaders(CakeEmail $email) {
|
2016-08-01 18:09:45 +00:00
|
|
|
return $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'to', 'cc', 'subject'));
|
2014-03-13 15:06:43 +00:00
|
|
|
}
|
2011-03-01 16:47:05 +00:00
|
|
|
|
2014-03-13 15:06:43 +00:00
|
|
|
/**
|
|
|
|
* Prepares the message body.
|
|
|
|
*
|
2016-08-01 21:46:25 +00:00
|
|
|
* @param CakeEmail $email CakeEmail
|
2014-03-13 15:06:43 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-01 21:46:25 +00:00
|
|
|
protected function _prepareMessage(CakeEmail $email) {
|
2016-08-01 18:09:45 +00:00
|
|
|
$lines = $email->message();
|
2012-05-07 22:34:14 +00:00
|
|
|
$messages = array();
|
|
|
|
foreach ($lines as $line) {
|
2012-05-08 15:16:17 +00:00
|
|
|
if ((!empty($line)) && ($line[0] === '.')) {
|
2012-05-07 22:34:14 +00:00
|
|
|
$messages[] = '.' . $line;
|
|
|
|
} else {
|
|
|
|
$messages[] = $line;
|
|
|
|
}
|
|
|
|
}
|
2014-03-13 15:06:43 +00:00
|
|
|
return implode("\r\n", $messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send emails
|
|
|
|
*
|
2016-08-01 21:46:25 +00:00
|
|
|
* @param CakeEmail $email CakeEmail
|
2014-03-13 15:06:43 +00:00
|
|
|
* @return void
|
|
|
|
* @throws SocketException
|
|
|
|
*/
|
2016-08-01 21:46:25 +00:00
|
|
|
protected function _sendRcpt(CakeEmail $email) {
|
2016-08-01 18:09:45 +00:00
|
|
|
$from = $this->_prepareFromAddress($email);
|
2014-03-13 15:06:43 +00:00
|
|
|
$this->_smtpSend($this->_prepareFromCmd(key($from)));
|
|
|
|
|
2016-08-01 18:09:45 +00:00
|
|
|
$emails = $this->_prepareRecipientAddresses($email);
|
2014-03-13 15:06:43 +00:00
|
|
|
foreach ($emails as $email) {
|
|
|
|
$this->_smtpSend($this->_prepareRcptCmd($email));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send Data
|
|
|
|
*
|
2016-08-01 21:46:25 +00:00
|
|
|
* @param CakeEmail $email CakeEmail
|
2014-03-13 15:06:43 +00:00
|
|
|
* @return void
|
|
|
|
* @throws SocketException
|
|
|
|
*/
|
2016-08-01 21:46:25 +00:00
|
|
|
protected function _sendData(CakeEmail $email) {
|
2014-03-13 15:06:43 +00:00
|
|
|
$this->_smtpSend('DATA', '354');
|
|
|
|
|
2016-08-01 18:09:45 +00:00
|
|
|
$headers = $this->_headersToString($this->_prepareMessageHeaders($email));
|
|
|
|
$message = $this->_prepareMessage($email);
|
2014-03-13 15:06:43 +00:00
|
|
|
|
2011-03-01 18:24:38 +00:00
|
|
|
$this->_smtpSend($headers . "\r\n\r\n" . $message . "\r\n\r\n\r\n.");
|
2011-08-26 00:31:18 +00:00
|
|
|
$this->_content = array('headers' => $headers, 'message' => $message);
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect
|
|
|
|
*
|
|
|
|
* @return void
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
|
|
|
protected function _disconnect() {
|
|
|
|
$this->_smtpSend('QUIT', false);
|
|
|
|
$this->_socket->disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to generate socket
|
|
|
|
*
|
|
|
|
* @return void
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
|
|
|
protected function _generateSocket() {
|
|
|
|
$this->_socket = new CakeSocket($this->_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Protected method for sending data to SMTP connection
|
|
|
|
*
|
2015-12-19 17:25:30 +00:00
|
|
|
* @param string|null $data Data to be sent to SMTP server
|
|
|
|
* @param string|bool $checkCode Code to check for in server response, false to skip
|
2015-12-19 19:07:13 +00:00
|
|
|
* @return string|null The matched code, or null if nothing matched
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
protected function _smtpSend($data, $checkCode = '250') {
|
2014-03-13 15:28:54 +00:00
|
|
|
$this->_lastResponse = array();
|
|
|
|
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($data !== null) {
|
2011-03-01 16:47:05 +00:00
|
|
|
$this->_socket->write($data . "\r\n");
|
|
|
|
}
|
|
|
|
while ($checkCode !== false) {
|
|
|
|
$response = '';
|
|
|
|
$startTime = time();
|
2011-04-04 02:41:40 +00:00
|
|
|
while (substr($response, -2) !== "\r\n" && ((time() - $startTime) < $this->_config['timeout'])) {
|
2017-02-14 03:37:44 +00:00
|
|
|
$bytes = $this->_socket->read();
|
|
|
|
if ($bytes === false || $bytes === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$response .= $bytes;
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
|
|
|
if (substr($response, -2) !== "\r\n") {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'SMTP timeout.'));
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
2011-11-14 01:47:55 +00:00
|
|
|
$responseLines = explode("\r\n", rtrim($response, "\r\n"));
|
|
|
|
$response = end($responseLines);
|
2011-03-01 16:47:05 +00:00
|
|
|
|
2014-03-13 15:28:54 +00:00
|
|
|
$this->_bufferResponseLines($responseLines);
|
|
|
|
|
2011-03-01 16:47:05 +00:00
|
|
|
if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) {
|
|
|
|
if ($code[2] === '-') {
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-19 17:25:30 +00:00
|
|
|
return $code[1];
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'SMTP Error: %s', $response));
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|