2011-03-01 16:47:05 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Send mail using mail() function
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://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.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-03-01 16:47:05 +00:00
|
|
|
* @link http://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
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-12-08 15:35:02 +00:00
|
|
|
* Send mail using mail() function
|
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 MailTransport extends AbstractTransport {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send mail
|
|
|
|
*
|
2011-07-29 03:56:10 +00:00
|
|
|
* @param CakeEmail $email CakeEmail
|
2011-08-26 00:31:18 +00:00
|
|
|
* @return array
|
2012-03-04 16:18:20 +00:00
|
|
|
* @throws SocketException When mail cannot be sent.
|
2011-03-01 16:47:05 +00:00
|
|
|
*/
|
|
|
|
public function send(CakeEmail $email) {
|
2011-04-13 03:34:23 +00:00
|
|
|
$eol = PHP_EOL;
|
|
|
|
if (isset($this->_config['eol'])) {
|
|
|
|
$eol = $this->_config['eol'];
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
2011-08-26 00:31:18 +00:00
|
|
|
$headers = $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'));
|
2011-03-01 18:24:38 +00:00
|
|
|
$to = $headers['To'];
|
|
|
|
unset($headers['To']);
|
2013-11-05 03:17:21 +00:00
|
|
|
foreach ($headers as $key => $header) {
|
|
|
|
$headers[$key] = str_replace(array("\r", "\n"), '', $header);
|
|
|
|
}
|
2011-08-26 00:31:18 +00:00
|
|
|
$headers = $this->_headersToString($headers, $eol);
|
2013-10-23 16:41:57 +00:00
|
|
|
$subject = str_replace(array("\r", "\n"), '', $email->subject());
|
2013-11-08 18:25:04 +00:00
|
|
|
$to = str_replace(array("\r", "\n"), '', $to);
|
|
|
|
|
|
|
|
$message = implode($eol, $email->message());
|
2012-09-09 13:32:50 +00:00
|
|
|
|
2012-12-13 17:16:30 +00:00
|
|
|
$params = isset($this->_config['additionalParameters']) ? $this->_config['additionalParameters'] : null;
|
2013-10-23 16:41:57 +00:00
|
|
|
$this->_mail($to, $subject, $message, $headers, $params);
|
2011-08-26 00:31:18 +00:00
|
|
|
return array('headers' => $headers, 'message' => $message);
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 13:32:50 +00:00
|
|
|
/**
|
|
|
|
* Wraps internal function mail() and throws exception instead of errors if anything goes wrong
|
|
|
|
*
|
|
|
|
* @param string $to email's recipient
|
|
|
|
* @param string $subject email's subject
|
|
|
|
* @param string $message email's body
|
|
|
|
* @param string $headers email's custom headers
|
2012-12-13 17:16:30 +00:00
|
|
|
* @param string $params additional params for sending email, will be ignored when in safe_mode
|
2012-09-09 13:32:50 +00:00
|
|
|
* @throws SocketException if mail could not be sent
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _mail($to, $subject, $message, $headers, $params = null) {
|
2012-12-13 17:16:30 +00:00
|
|
|
if (ini_get('safe_mode')) {
|
|
|
|
//@codingStandardsIgnoreStart
|
|
|
|
if (!@mail($to, $subject, $message, $headers)) {
|
2014-03-03 02:07:25 +00:00
|
|
|
$error = error_get_last();
|
|
|
|
$msg = 'Could not send email: ' . isset($error['message']) ? $error['message'] : 'unknown';
|
|
|
|
throw new SocketException($msg);
|
2012-12-13 17:16:30 +00:00
|
|
|
}
|
|
|
|
} elseif (!@mail($to, $subject, $message, $headers, $params)) {
|
2014-03-03 02:07:25 +00:00
|
|
|
$error = error_get_last();
|
|
|
|
$msg = 'Could not send email: ' . isset($error['message']) ? $error['message'] : 'unknown';
|
2012-12-13 17:16:30 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
2014-03-03 02:07:25 +00:00
|
|
|
throw new SocketException($msg);
|
2012-09-09 13:32:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 16:47:05 +00:00
|
|
|
}
|