2011-02-27 18:25:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2013-10-09 23:38:16 +00:00
|
|
|
* CakePHP Email
|
2011-02-27 18:25:23 +00:00
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* 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-02-27 18:25:23 +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-02-27 18:25:23 +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-02-27 18:25:23 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Network.Email
|
2011-02-27 18:25:23 +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-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
2011-04-13 03:29:34 +00:00
|
|
|
App::uses('Validation', 'Utility');
|
|
|
|
App::uses('Multibyte', 'I18n');
|
|
|
|
App::uses('AbstractTransport', 'Network/Email');
|
2013-05-16 20:15:23 +00:00
|
|
|
App::uses('File', 'Utility');
|
2011-04-16 02:02:07 +00:00
|
|
|
App::uses('String', 'Utility');
|
|
|
|
App::uses('View', 'View');
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
2013-10-09 23:38:16 +00:00
|
|
|
* CakePHP email class.
|
2011-02-27 18:25:23 +00:00
|
|
|
*
|
|
|
|
* This class is used for handling Internet Message Format based
|
|
|
|
* based on the standard outlined in http://www.rfc-editor.org/rfc/rfc2822.txt
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Network.Email
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
|
|
|
class CakeEmail {
|
2012-11-28 22:30:47 +00:00
|
|
|
|
2011-03-01 04:19:47 +00:00
|
|
|
/**
|
2011-04-23 02:41:52 +00:00
|
|
|
* Default X-Mailer
|
2011-03-01 04:19:47 +00:00
|
|
|
*
|
|
|
|
* @constant EMAIL_CLIENT
|
|
|
|
*/
|
2011-04-23 02:41:52 +00:00
|
|
|
const EMAIL_CLIENT = 'CakePHP Email';
|
2011-03-01 04:19:47 +00:00
|
|
|
|
2011-03-02 21:17:49 +00:00
|
|
|
/**
|
|
|
|
* Line length - no should more - RFC 2822 - 2.1.1
|
|
|
|
*
|
|
|
|
* @constant LINE_LENGTH_SHOULD
|
|
|
|
*/
|
|
|
|
const LINE_LENGTH_SHOULD = 78;
|
|
|
|
|
|
|
|
/**
|
2011-04-17 11:13:02 +00:00
|
|
|
* Line length - no must more - RFC 2822 - 2.1.1
|
2011-03-02 21:17:49 +00:00
|
|
|
*
|
|
|
|
* @constant LINE_LENGTH_MUST
|
|
|
|
*/
|
|
|
|
const LINE_LENGTH_MUST = 998;
|
|
|
|
|
2011-04-17 21:53:51 +00:00
|
|
|
/**
|
|
|
|
* Type of message - HTML
|
|
|
|
*
|
|
|
|
* @constant MESSAGE_HTML
|
|
|
|
*/
|
|
|
|
const MESSAGE_HTML = 'html';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type of message - TEXT
|
|
|
|
*
|
|
|
|
* @constant MESSAGE_TEXT
|
|
|
|
*/
|
|
|
|
const MESSAGE_TEXT = 'text';
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
|
|
|
* Recipient of the email
|
|
|
|
*
|
2011-04-19 23:48:52 +00:00
|
|
|
* @var array
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-03-01 04:19:47 +00:00
|
|
|
protected $_to = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The mail which the email is sent from
|
|
|
|
*
|
2011-04-19 23:48:52 +00:00
|
|
|
* @var array
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-03-01 04:19:47 +00:00
|
|
|
protected $_from = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
2011-04-20 00:18:02 +00:00
|
|
|
/**
|
|
|
|
* The sender email
|
|
|
|
*
|
2012-11-28 22:30:47 +00:00
|
|
|
* @var array
|
2011-04-20 00:18:02 +00:00
|
|
|
*/
|
|
|
|
protected $_sender = array();
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
|
|
|
* The email the recipient will reply to
|
|
|
|
*
|
2011-04-19 23:48:52 +00:00
|
|
|
* @var array
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-03-01 17:09:08 +00:00
|
|
|
protected $_replyTo = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The read receipt email
|
|
|
|
*
|
2011-04-19 23:48:52 +00:00
|
|
|
* @var array
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-03-01 17:09:08 +00:00
|
|
|
protected $_readReceipt = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The mail that will be used in case of any errors like
|
|
|
|
* - Remote mailserver down
|
|
|
|
* - Remote user has exceeded his quota
|
|
|
|
* - Unknown user
|
|
|
|
*
|
2011-04-19 23:48:52 +00:00
|
|
|
* @var array
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-03-01 17:09:08 +00:00
|
|
|
protected $_returnPath = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Carbon Copy
|
|
|
|
*
|
|
|
|
* List of email's that should receive a copy of the email.
|
|
|
|
* The Recipient WILL be able to see this list
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-03-01 04:19:47 +00:00
|
|
|
protected $_cc = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Blind Carbon Copy
|
|
|
|
*
|
|
|
|
* List of email's that should receive a copy of the email.
|
|
|
|
* The Recipient WILL NOT be able to see this list
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-03-01 04:19:47 +00:00
|
|
|
protected $_bcc = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
2011-03-01 23:57:45 +00:00
|
|
|
/**
|
|
|
|
* Message ID
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @var boolean|string True to generate, False to ignore, String with value
|
2011-03-01 23:57:45 +00:00
|
|
|
*/
|
|
|
|
protected $_messageId = true;
|
2012-04-21 00:52:10 +00:00
|
|
|
|
2012-04-20 01:38:26 +00:00
|
|
|
/**
|
|
|
|
* Domain for messageId generation.
|
|
|
|
* Needs to be manually set for CLI mailing as env('HTTP_HOST') is empty
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_domain = null;
|
2011-03-01 23:57:45 +00:00
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
|
|
|
* The subject of the email
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-03-01 17:20:13 +00:00
|
|
|
protected $_subject = '';
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Associative array of a user defined headers
|
|
|
|
* Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-03-01 04:19:47 +00:00
|
|
|
protected $_headers = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Layout for the View
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-03-01 21:16:55 +00:00
|
|
|
protected $_layout = 'default';
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Template for the view
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-03-01 21:16:55 +00:00
|
|
|
protected $_template = '';
|
2011-02-27 18:25:23 +00:00
|
|
|
|
2011-03-04 11:29:38 +00:00
|
|
|
/**
|
|
|
|
* View for render
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_viewRender = 'View';
|
|
|
|
|
2011-04-17 21:22:20 +00:00
|
|
|
/**
|
|
|
|
* Vars to sent to render
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_viewVars = array();
|
|
|
|
|
2012-03-28 05:22:55 +00:00
|
|
|
/**
|
|
|
|
* Theme for the View
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_theme = null;
|
|
|
|
|
2011-06-19 22:43:11 +00:00
|
|
|
/**
|
|
|
|
* Helpers to be used in the render
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-10-02 02:55:04 +00:00
|
|
|
protected $_helpers = array('Html');
|
2011-06-19 22:43:11 +00:00
|
|
|
|
2011-03-02 21:17:49 +00:00
|
|
|
/**
|
|
|
|
* Text message
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_textMessage = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Html message
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_htmlMessage = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Final message to send
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_message = array();
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
2011-03-01 21:25:57 +00:00
|
|
|
* Available formats to be sent.
|
2011-02-27 18:25:23 +00:00
|
|
|
*
|
2011-03-01 21:25:57 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_emailFormatAvailable = array('text', 'html', 'both');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What format should the email be sent in
|
2011-02-27 18:25:23 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-03-01 21:25:57 +00:00
|
|
|
protected $_emailFormat = 'text';
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* What method should the email be sent
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-05-24 04:56:28 +00:00
|
|
|
protected $_transportName = 'Mail';
|
2011-02-27 18:25:23 +00:00
|
|
|
|
2011-04-13 04:04:37 +00:00
|
|
|
/**
|
2011-04-19 18:02:35 +00:00
|
|
|
* Instance of transport class
|
2011-04-13 04:04:37 +00:00
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @var AbstractTransport
|
2011-04-13 04:04:37 +00:00
|
|
|
*/
|
|
|
|
protected $_transportClass = null;
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
2011-10-16 20:11:59 +00:00
|
|
|
* Charset the email body is sent in
|
|
|
|
*
|
2011-02-27 18:25:23 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $charset = 'utf-8';
|
|
|
|
|
2011-10-16 20:11:59 +00:00
|
|
|
/**
|
|
|
|
* Charset the email header is sent in
|
|
|
|
* If null, the $charset property will be used as default
|
2012-03-04 16:18:20 +00:00
|
|
|
*
|
2011-10-16 20:11:59 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $headerCharset = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The application wide charset, used to encode headers and body
|
2012-03-04 16:18:20 +00:00
|
|
|
*
|
2011-10-16 20:11:59 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2012-03-04 16:18:20 +00:00
|
|
|
protected $_appCharset = null;
|
2011-10-16 20:11:59 +00:00
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
|
|
|
* List of files that should be attached to the email.
|
|
|
|
*
|
2011-03-01 21:51:12 +00:00
|
|
|
* Only absolute paths
|
2011-02-27 18:25:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-03-01 21:51:12 +00:00
|
|
|
protected $_attachments = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If set, boundary to use for multipart mime messages
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_boundary = null;
|
|
|
|
|
2011-04-10 01:56:09 +00:00
|
|
|
/**
|
|
|
|
* Configuration to transport
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @var string|array
|
2011-04-10 01:56:09 +00:00
|
|
|
*/
|
2011-08-25 14:03:39 +00:00
|
|
|
protected $_config = array();
|
2011-04-10 01:56:09 +00:00
|
|
|
|
2011-10-19 05:02:31 +00:00
|
|
|
/**
|
|
|
|
* 8Bit character sets
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_charset8bit = array('UTF-8', 'SHIFT_JIS');
|
|
|
|
|
2012-05-21 10:21:02 +00:00
|
|
|
/**
|
2012-05-22 00:23:49 +00:00
|
|
|
* Define Content-Type charset name
|
2012-05-21 10:21:02 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-05-22 00:23:49 +00:00
|
|
|
protected $_contentTypeCharset = array(
|
|
|
|
'ISO-2022-JP-MS' => 'ISO-2022-JP'
|
|
|
|
);
|
2012-05-21 10:21:02 +00:00
|
|
|
|
2013-06-28 17:44:55 +00:00
|
|
|
/**
|
|
|
|
* Regex for email validation
|
|
|
|
* If null, it will use built in regex
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-06-30 15:03:18 +00:00
|
|
|
protected $_emailPattern = null;
|
2013-06-28 17:44:55 +00:00
|
|
|
|
2013-09-28 02:22:23 +00:00
|
|
|
/**
|
|
|
|
* The classname used for email configuration.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_configClass = 'EmailConfig';
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2013-04-29 14:18:00 +00:00
|
|
|
* @param array|string $config Array of configs, or string to load configs from email.php
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-08-17 21:15:57 +00:00
|
|
|
public function __construct($config = null) {
|
2011-10-16 20:11:59 +00:00
|
|
|
$this->_appCharset = Configure::read('App.encoding');
|
|
|
|
if ($this->_appCharset !== null) {
|
|
|
|
$this->charset = $this->_appCharset;
|
2011-02-27 18:25:23 +00:00
|
|
|
}
|
2012-09-29 01:20:14 +00:00
|
|
|
$this->_domain = preg_replace('/\:\d+$/', '', env('HTTP_HOST'));
|
2012-04-20 01:38:26 +00:00
|
|
|
if (empty($this->_domain)) {
|
|
|
|
$this->_domain = php_uname('n');
|
|
|
|
}
|
2012-04-21 00:52:10 +00:00
|
|
|
|
2011-08-17 21:15:57 +00:00
|
|
|
if ($config) {
|
|
|
|
$this->config($config);
|
|
|
|
}
|
2011-10-16 20:11:59 +00:00
|
|
|
if (empty($this->headerCharset)) {
|
|
|
|
$this->headerCharset = $this->charset;
|
|
|
|
}
|
2011-02-27 18:25:23 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 04:31:30 +00:00
|
|
|
/**
|
2011-03-21 12:18:36 +00:00
|
|
|
* From
|
2011-03-01 04:31:30 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email
|
2011-03-01 04:31:30 +00:00
|
|
|
* @param string $name
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 04:31:30 +00:00
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function from($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_from;
|
|
|
|
}
|
2011-10-15 01:01:17 +00:00
|
|
|
return $this->_setEmailSingle('_from', $email, $name, __d('cake_dev', 'From requires only 1 email address.'));
|
2011-03-01 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2011-04-20 00:18:02 +00:00
|
|
|
/**
|
|
|
|
* Sender
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email
|
2011-04-20 00:18:02 +00:00
|
|
|
* @param string $name
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-04-20 00:18:02 +00:00
|
|
|
*/
|
|
|
|
public function sender($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_sender;
|
|
|
|
}
|
2011-10-15 01:01:17 +00:00
|
|
|
return $this->_setEmailSingle('_sender', $email, $name, __d('cake_dev', 'Sender requires only 1 email address.'));
|
2011-04-20 00:18:02 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 17:09:08 +00:00
|
|
|
/**
|
2011-03-21 12:18:36 +00:00
|
|
|
* Reply-To
|
2011-03-01 17:09:08 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email
|
2011-03-01 17:09:08 +00:00
|
|
|
* @param string $name
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 17:09:08 +00:00
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function replyTo($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_replyTo;
|
|
|
|
}
|
2011-10-15 01:01:17 +00:00
|
|
|
return $this->_setEmailSingle('_replyTo', $email, $name, __d('cake_dev', 'Reply-To requires only 1 email address.'));
|
2011-03-01 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-21 12:18:36 +00:00
|
|
|
* Read Receipt (Disposition-Notification-To header)
|
2011-03-01 17:09:08 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email
|
2011-03-01 17:09:08 +00:00
|
|
|
* @param string $name
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 17:09:08 +00:00
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function readReceipt($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_readReceipt;
|
|
|
|
}
|
2011-10-15 01:01:17 +00:00
|
|
|
return $this->_setEmailSingle('_readReceipt', $email, $name, __d('cake_dev', 'Disposition-Notification-To requires only 1 email address.'));
|
2011-03-01 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-21 12:18:36 +00:00
|
|
|
* Return Path
|
2011-03-01 17:09:08 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email
|
2011-03-01 17:09:08 +00:00
|
|
|
* @param string $name
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 17:09:08 +00:00
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function returnPath($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_returnPath;
|
|
|
|
}
|
2011-10-15 01:01:17 +00:00
|
|
|
return $this->_setEmailSingle('_returnPath', $email, $name, __d('cake_dev', 'Return-Path requires only 1 email address.'));
|
2011-03-01 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 05:09:05 +00:00
|
|
|
/**
|
2011-03-21 12:18:36 +00:00
|
|
|
* To
|
2011-03-01 05:09:05 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email Null to get, String with email, Array with email as key, name as value or email as value (without name)
|
2011-03-01 05:09:05 +00:00
|
|
|
* @param string $name
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-03-01 05:09:05 +00:00
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function to($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_to;
|
|
|
|
}
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_setEmail('_to', $email, $name);
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add To
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
|
2011-03-01 05:09:05 +00:00
|
|
|
* @param string $name
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail $this
|
2011-03-01 05:09:05 +00:00
|
|
|
*/
|
|
|
|
public function addTo($email, $name = null) {
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_addEmail('_to', $email, $name);
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-21 12:18:36 +00:00
|
|
|
* Cc
|
2011-03-01 16:35:28 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
|
2011-03-01 16:35:28 +00:00
|
|
|
* @param string $name
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-03-01 16:35:28 +00:00
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function cc($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_cc;
|
|
|
|
}
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_setEmail('_cc', $email, $name);
|
2011-03-01 16:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Cc
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
|
2011-03-01 16:35:28 +00:00
|
|
|
* @param string $name
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail $this
|
2011-03-01 16:35:28 +00:00
|
|
|
*/
|
|
|
|
public function addCc($email, $name = null) {
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_addEmail('_cc', $email, $name);
|
2011-03-01 16:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-21 12:18:36 +00:00
|
|
|
* Bcc
|
2011-03-01 16:35:28 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
|
2011-03-01 16:35:28 +00:00
|
|
|
* @param string $name
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-03-01 16:35:28 +00:00
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function bcc($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_bcc;
|
|
|
|
}
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_setEmail('_bcc', $email, $name);
|
2011-03-01 16:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Bcc
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
|
2011-03-01 16:35:28 +00:00
|
|
|
* @param string $name
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail $this
|
2011-03-01 16:35:28 +00:00
|
|
|
*/
|
|
|
|
public function addBcc($email, $name = null) {
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_addEmail('_bcc', $email, $name);
|
2011-03-01 16:35:28 +00:00
|
|
|
}
|
|
|
|
|
2012-03-16 12:54:30 +00:00
|
|
|
/**
|
|
|
|
* Charset setter/getter
|
|
|
|
*
|
|
|
|
* @param string $charset
|
|
|
|
* @return string $this->charset
|
|
|
|
*/
|
|
|
|
public function charset($charset = null) {
|
|
|
|
if ($charset === null) {
|
|
|
|
return $this->charset;
|
|
|
|
}
|
|
|
|
$this->charset = $charset;
|
|
|
|
if (empty($this->headerCharset)) {
|
|
|
|
$this->headerCharset = $charset;
|
|
|
|
}
|
|
|
|
return $this->charset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HeaderCharset setter/getter
|
|
|
|
*
|
|
|
|
* @param string $charset
|
|
|
|
* @return string $this->charset
|
|
|
|
*/
|
|
|
|
public function headerCharset($charset = null) {
|
|
|
|
if ($charset === null) {
|
|
|
|
return $this->headerCharset;
|
|
|
|
}
|
|
|
|
return $this->headerCharset = $charset;
|
|
|
|
}
|
|
|
|
|
2013-06-28 17:44:55 +00:00
|
|
|
/**
|
2013-06-30 15:03:18 +00:00
|
|
|
* EmailPattern setter/getter
|
2013-06-28 17:44:55 +00:00
|
|
|
*
|
2013-06-30 15:03:18 +00:00
|
|
|
* @param string $regex for email address validation
|
2013-06-28 17:44:55 +00:00
|
|
|
* @return string|CakeEmail
|
|
|
|
*/
|
2013-06-30 15:03:18 +00:00
|
|
|
public function emailPattern($regex = null) {
|
2013-06-29 15:51:02 +00:00
|
|
|
if ($regex === null) {
|
2013-06-30 15:03:18 +00:00
|
|
|
return $this->_emailPattern;
|
2013-06-28 17:44:55 +00:00
|
|
|
}
|
2013-06-30 15:03:18 +00:00
|
|
|
$this->_emailPattern = $regex;
|
2013-06-28 17:44:55 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-03-01 05:09:05 +00:00
|
|
|
/**
|
|
|
|
* Set email
|
|
|
|
*
|
|
|
|
* @param string $varName
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email
|
|
|
|
* @param string $name
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail $this
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 05:09:05 +00:00
|
|
|
*/
|
|
|
|
protected function _setEmail($varName, $email, $name) {
|
|
|
|
if (!is_array($email)) {
|
2013-06-30 15:03:18 +00:00
|
|
|
if (!Validation::email($email, false, $this->_emailPattern)) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
|
2011-03-01 22:46:20 +00:00
|
|
|
}
|
2011-03-01 05:09:05 +00:00
|
|
|
if ($name === null) {
|
2011-03-01 22:58:04 +00:00
|
|
|
$name = $email;
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
2011-03-01 22:58:04 +00:00
|
|
|
$this->{$varName} = array($email => $name);
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
|
|
|
$list = array();
|
|
|
|
foreach ($email as $key => $value) {
|
|
|
|
if (is_int($key)) {
|
2011-03-01 22:58:04 +00:00
|
|
|
$key = $value;
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
2013-06-30 15:03:18 +00:00
|
|
|
if (!Validation::email($key, false, $this->_emailPattern)) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
|
2011-03-01 22:58:04 +00:00
|
|
|
}
|
|
|
|
$list[$key] = $value;
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
|
|
|
$this->{$varName} = $list;
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 17:09:08 +00:00
|
|
|
/**
|
|
|
|
* Set only 1 email
|
|
|
|
*
|
|
|
|
* @param string $varName
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email
|
2011-03-01 17:09:08 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param string $throwMessage
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail $this
|
2011-07-31 20:55:52 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 17:09:08 +00:00
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
protected function _setEmailSingle($varName, $email, $name, $throwMessage) {
|
2011-03-01 17:09:08 +00:00
|
|
|
$current = $this->{$varName};
|
|
|
|
$this->_setEmail($varName, $email, $name);
|
|
|
|
if (count($this->{$varName}) !== 1) {
|
|
|
|
$this->{$varName} = $current;
|
|
|
|
throw new SocketException($throwMessage);
|
|
|
|
}
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 05:09:05 +00:00
|
|
|
/**
|
|
|
|
* Add email
|
|
|
|
*
|
|
|
|
* @param string $varName
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $email
|
|
|
|
* @param string $name
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail $this
|
2011-07-31 20:55:52 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 05:09:05 +00:00
|
|
|
*/
|
|
|
|
protected function _addEmail($varName, $email, $name) {
|
|
|
|
if (!is_array($email)) {
|
2013-06-30 15:03:18 +00:00
|
|
|
if (!Validation::email($email, false, $this->_emailPattern)) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
|
2011-03-01 22:46:20 +00:00
|
|
|
}
|
2011-03-01 05:09:05 +00:00
|
|
|
if ($name === null) {
|
2011-03-01 22:58:04 +00:00
|
|
|
$name = $email;
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
2011-03-01 22:58:04 +00:00
|
|
|
$this->{$varName}[$email] = $name;
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
|
|
|
$list = array();
|
|
|
|
foreach ($email as $key => $value) {
|
|
|
|
if (is_int($key)) {
|
2011-03-01 22:58:04 +00:00
|
|
|
$key = $value;
|
|
|
|
}
|
2013-06-30 15:03:18 +00:00
|
|
|
if (!Validation::email($key, false, $this->_emailPattern)) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
2011-03-01 22:58:04 +00:00
|
|
|
$list[$key] = $value;
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
|
|
|
$this->{$varName} = array_merge($this->{$varName}, $list);
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 05:09:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 17:20:13 +00:00
|
|
|
/**
|
2011-11-14 01:47:55 +00:00
|
|
|
* Get/Set Subject.
|
2011-03-01 17:20:13 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string $subject
|
|
|
|
* @return string|CakeEmail
|
2011-03-01 17:20:13 +00:00
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function subject($subject = null) {
|
|
|
|
if ($subject === null) {
|
|
|
|
return $this->_subject;
|
|
|
|
}
|
2011-04-17 18:00:19 +00:00
|
|
|
$this->_subject = $this->_encode((string)$subject);
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 17:20:13 +00:00
|
|
|
}
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
2011-04-19 18:02:35 +00:00
|
|
|
* Sets headers for the message
|
2011-02-27 18:25:23 +00:00
|
|
|
*
|
2011-07-29 03:56:10 +00:00
|
|
|
* @param array $headers Associative array containing headers to be set.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail $this
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-03-01 04:19:47 +00:00
|
|
|
public function setHeaders($headers) {
|
2011-02-27 18:57:04 +00:00
|
|
|
if (!is_array($headers)) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', '$headers should be an array.'));
|
2011-02-27 18:57:04 +00:00
|
|
|
}
|
2011-03-01 04:19:47 +00:00
|
|
|
$this->_headers = $headers;
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 04:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add header for the message
|
|
|
|
*
|
|
|
|
* @param array $headers
|
2011-08-24 14:58:51 +00:00
|
|
|
* @return object $this
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 04:19:47 +00:00
|
|
|
*/
|
|
|
|
public function addHeaders($headers) {
|
|
|
|
if (!is_array($headers)) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', '$headers should be an array.'));
|
2011-03-01 04:19:47 +00:00
|
|
|
}
|
|
|
|
$this->_headers = array_merge($this->_headers, $headers);
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 04:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of headers
|
|
|
|
*
|
2011-03-01 18:16:50 +00:00
|
|
|
* ### Includes:
|
|
|
|
*
|
|
|
|
* - `from`
|
|
|
|
* - `replyTo`
|
|
|
|
* - `readReceipt`
|
|
|
|
* - `returnPath`
|
|
|
|
* - `to`
|
|
|
|
* - `cc`
|
|
|
|
* - `bcc`
|
|
|
|
* - `subject`
|
|
|
|
*
|
|
|
|
* @param array $include
|
2011-03-01 04:19:47 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2011-03-01 18:16:50 +00:00
|
|
|
public function getHeaders($include = array()) {
|
2011-08-26 00:31:18 +00:00
|
|
|
if ($include == array_values($include)) {
|
|
|
|
$include = array_fill_keys($include, true);
|
|
|
|
}
|
|
|
|
$defaults = array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), false);
|
2011-03-01 18:16:50 +00:00
|
|
|
$include += $defaults;
|
|
|
|
|
2011-03-02 00:10:34 +00:00
|
|
|
$headers = array();
|
2011-03-01 18:16:50 +00:00
|
|
|
$relation = array(
|
|
|
|
'from' => 'From',
|
|
|
|
'replyTo' => 'Reply-To',
|
|
|
|
'readReceipt' => 'Disposition-Notification-To',
|
|
|
|
'returnPath' => 'Return-Path'
|
|
|
|
);
|
|
|
|
foreach ($relation as $var => $header) {
|
|
|
|
if ($include[$var]) {
|
|
|
|
$var = '_' . $var;
|
|
|
|
$headers[$header] = current($this->_formatAddress($this->{$var}));
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 00:18:02 +00:00
|
|
|
if ($include['sender']) {
|
|
|
|
if (key($this->_sender) === key($this->_from)) {
|
|
|
|
$headers['Sender'] = '';
|
|
|
|
} else {
|
|
|
|
$headers['Sender'] = current($this->_formatAddress($this->_sender));
|
|
|
|
}
|
|
|
|
}
|
2011-03-01 18:16:50 +00:00
|
|
|
|
|
|
|
foreach (array('to', 'cc', 'bcc') as $var) {
|
|
|
|
if ($include[$var]) {
|
|
|
|
$classVar = '_' . $var;
|
|
|
|
$headers[ucfirst($var)] = implode(', ', $this->_formatAddress($this->{$classVar}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-02 00:10:34 +00:00
|
|
|
$headers += $this->_headers;
|
|
|
|
if (!isset($headers['X-Mailer'])) {
|
2011-04-10 03:09:08 +00:00
|
|
|
$headers['X-Mailer'] = self::EMAIL_CLIENT;
|
2011-03-02 00:10:34 +00:00
|
|
|
}
|
|
|
|
if (!isset($headers['Date'])) {
|
|
|
|
$headers['Date'] = date(DATE_RFC2822);
|
|
|
|
}
|
|
|
|
if ($this->_messageId !== false) {
|
|
|
|
if ($this->_messageId === true) {
|
2012-04-20 01:38:26 +00:00
|
|
|
$headers['Message-ID'] = '<' . str_replace('-', '', String::UUID()) . '@' . $this->_domain . '>';
|
2011-03-02 00:10:34 +00:00
|
|
|
} else {
|
|
|
|
$headers['Message-ID'] = $this->_messageId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:16:50 +00:00
|
|
|
if ($include['subject']) {
|
|
|
|
$headers['Subject'] = $this->_subject;
|
|
|
|
}
|
|
|
|
|
2011-04-20 01:47:48 +00:00
|
|
|
$headers['MIME-Version'] = '1.0';
|
2011-12-28 04:38:21 +00:00
|
|
|
if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
|
2011-03-04 02:05:30 +00:00
|
|
|
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
|
|
|
|
} elseif ($this->_emailFormat === 'text') {
|
2012-05-21 10:21:02 +00:00
|
|
|
$headers['Content-Type'] = 'text/plain; charset=' . $this->_getContentTypeCharset();
|
2011-03-04 02:05:30 +00:00
|
|
|
} elseif ($this->_emailFormat === 'html') {
|
2012-05-21 10:21:02 +00:00
|
|
|
$headers['Content-Type'] = 'text/html; charset=' . $this->_getContentTypeCharset();
|
2011-03-04 02:05:30 +00:00
|
|
|
}
|
2011-10-22 04:24:26 +00:00
|
|
|
$headers['Content-Transfer-Encoding'] = $this->_getContentTransferEncoding();
|
2011-03-04 02:05:30 +00:00
|
|
|
|
2011-03-01 18:16:50 +00:00
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format addresses
|
|
|
|
*
|
2013-09-13 00:14:11 +00:00
|
|
|
* If the address contains non alphanumeric/whitespace characters, it will
|
|
|
|
* be quoted as characters like `:` and `,` are known to cause issues
|
|
|
|
* in address header fields.
|
|
|
|
*
|
2011-03-01 18:16:50 +00:00
|
|
|
* @param array $address
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _formatAddress($address) {
|
|
|
|
$return = array();
|
|
|
|
foreach ($address as $email => $alias) {
|
|
|
|
if ($email === $alias) {
|
|
|
|
$return[] = $email;
|
|
|
|
} else {
|
2013-09-13 00:14:11 +00:00
|
|
|
$encoded = $this->_encode($alias);
|
|
|
|
if ($encoded === $alias && preg_match('/[^a-z0-9 ]/i', $encoded)) {
|
|
|
|
$encoded = '"' . str_replace('"', '\"', $encoded) . '"';
|
2012-02-11 15:15:53 +00:00
|
|
|
}
|
2013-09-13 00:14:11 +00:00
|
|
|
$return[] = sprintf('%s <%s>', $encoded, $email);
|
2011-03-01 18:16:50 +00:00
|
|
|
}
|
2011-02-27 18:25:23 +00:00
|
|
|
}
|
2011-03-01 18:16:50 +00:00
|
|
|
return $return;
|
2011-02-27 18:25:23 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 21:16:55 +00:00
|
|
|
/**
|
2011-04-17 23:40:18 +00:00
|
|
|
* Template and layout
|
2011-03-01 21:16:55 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param boolean|string $template Template name or null to not use
|
|
|
|
* @param boolean|string $layout Layout name or null to not use
|
|
|
|
* @return array|CakeEmail
|
2011-03-01 21:16:55 +00:00
|
|
|
*/
|
2011-04-17 23:40:18 +00:00
|
|
|
public function template($template = false, $layout = false) {
|
|
|
|
if ($template === false) {
|
2011-03-21 12:30:16 +00:00
|
|
|
return array(
|
2011-04-17 23:40:18 +00:00
|
|
|
'template' => $this->_template,
|
|
|
|
'layout' => $this->_layout
|
2011-03-21 12:30:16 +00:00
|
|
|
);
|
|
|
|
}
|
2011-04-17 23:40:18 +00:00
|
|
|
$this->_template = $template;
|
|
|
|
if ($layout !== false) {
|
|
|
|
$this->_layout = $layout;
|
2011-03-01 21:16:55 +00:00
|
|
|
}
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 11:29:38 +00:00
|
|
|
/**
|
2011-03-21 12:30:16 +00:00
|
|
|
* View class for render
|
2011-03-04 11:29:38 +00:00
|
|
|
*
|
|
|
|
* @param string $viewClass
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return string|CakeEmail
|
2011-03-04 11:29:38 +00:00
|
|
|
*/
|
2011-03-21 12:30:16 +00:00
|
|
|
public function viewRender($viewClass = null) {
|
|
|
|
if ($viewClass === null) {
|
|
|
|
return $this->_viewRender;
|
|
|
|
}
|
2011-03-04 11:29:38 +00:00
|
|
|
$this->_viewRender = $viewClass;
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-04 11:29:38 +00:00
|
|
|
}
|
|
|
|
|
2011-04-17 21:22:20 +00:00
|
|
|
/**
|
|
|
|
* Variables to be set on render
|
|
|
|
*
|
2011-04-18 00:57:57 +00:00
|
|
|
* @param array $viewVars
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-04-17 21:22:20 +00:00
|
|
|
*/
|
|
|
|
public function viewVars($viewVars = null) {
|
|
|
|
if ($viewVars === null) {
|
|
|
|
return $this->_viewVars;
|
|
|
|
}
|
2011-04-18 00:57:57 +00:00
|
|
|
$this->_viewVars = array_merge($this->_viewVars, (array)$viewVars);
|
2011-04-17 21:22:20 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-03-28 05:22:55 +00:00
|
|
|
/**
|
|
|
|
* Theme to use when rendering
|
|
|
|
*
|
|
|
|
* @param string $theme
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return string|CakeEmail
|
2012-03-28 05:22:55 +00:00
|
|
|
*/
|
|
|
|
public function theme($theme = null) {
|
|
|
|
if ($theme === null) {
|
|
|
|
return $this->_theme;
|
|
|
|
}
|
|
|
|
$this->_theme = $theme;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-06-19 22:43:11 +00:00
|
|
|
/**
|
|
|
|
* Helpers to be used in render
|
|
|
|
*
|
|
|
|
* @param array $helpers
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return array|CakeEmail
|
2011-06-19 22:43:11 +00:00
|
|
|
*/
|
|
|
|
public function helpers($helpers = null) {
|
|
|
|
if ($helpers === null) {
|
|
|
|
return $this->_helpers;
|
|
|
|
}
|
|
|
|
$this->_helpers = (array)$helpers;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-03-01 21:25:57 +00:00
|
|
|
/**
|
2011-03-21 12:30:16 +00:00
|
|
|
* Email format
|
2011-03-01 21:25:57 +00:00
|
|
|
*
|
|
|
|
* @param string $format
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return string|CakeEmail
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 21:25:57 +00:00
|
|
|
*/
|
2011-03-21 12:30:16 +00:00
|
|
|
public function emailFormat($format = null) {
|
|
|
|
if ($format === null) {
|
|
|
|
return $this->_emailFormat;
|
|
|
|
}
|
2011-03-01 21:25:57 +00:00
|
|
|
if (!in_array($format, $this->_emailFormatAvailable)) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'Format not available.'));
|
2011-03-01 21:25:57 +00:00
|
|
|
}
|
|
|
|
$this->_emailFormat = $format;
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 21:25:57 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 21:29:46 +00:00
|
|
|
/**
|
2011-03-21 12:30:16 +00:00
|
|
|
* Transport name
|
2011-03-01 21:29:46 +00:00
|
|
|
*
|
|
|
|
* @param string $name
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return string|CakeEmail
|
2011-03-01 21:29:46 +00:00
|
|
|
*/
|
2011-03-21 12:30:16 +00:00
|
|
|
public function transport($name = null) {
|
|
|
|
if ($name === null) {
|
|
|
|
return $this->_transportName;
|
|
|
|
}
|
2011-03-01 21:29:46 +00:00
|
|
|
$this->_transportName = (string)$name;
|
2011-04-13 04:04:37 +00:00
|
|
|
$this->_transportClass = null;
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2011-04-13 04:04:37 +00:00
|
|
|
/**
|
|
|
|
* Return the transport class
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-04-13 04:04:37 +00:00
|
|
|
*/
|
|
|
|
public function transportClass() {
|
|
|
|
if ($this->_transportClass) {
|
|
|
|
return $this->_transportClass;
|
|
|
|
}
|
|
|
|
list($plugin, $transportClassname) = pluginSplit($this->_transportName, true);
|
|
|
|
$transportClassname .= 'Transport';
|
|
|
|
App::uses($transportClassname, $plugin . 'Network/Email');
|
|
|
|
if (!class_exists($transportClassname)) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'Class "%s" not found.', $transportClassname));
|
2011-04-13 04:04:37 +00:00
|
|
|
} elseif (!method_exists($transportClassname, 'send')) {
|
2013-08-16 09:39:38 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'The "%s" does not have a %s method.', $transportClassname, 'send()'));
|
2011-04-13 04:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_transportClass = new $transportClassname();
|
|
|
|
}
|
|
|
|
|
2011-03-01 23:57:45 +00:00
|
|
|
/**
|
2011-03-21 12:30:16 +00:00
|
|
|
* Message-ID
|
2011-03-01 23:57:45 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param boolean|string $message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID
|
|
|
|
* @return boolean|string|CakeEmail
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 23:57:45 +00:00
|
|
|
*/
|
2011-04-18 01:23:48 +00:00
|
|
|
public function messageId($message = null) {
|
2011-03-21 12:30:16 +00:00
|
|
|
if ($message === null) {
|
|
|
|
return $this->_messageId;
|
|
|
|
}
|
2011-03-01 23:57:45 +00:00
|
|
|
if (is_bool($message)) {
|
|
|
|
$this->_messageId = $message;
|
|
|
|
} else {
|
|
|
|
if (!preg_match('/^\<.+@.+\>$/', $message)) {
|
2012-02-19 16:47:49 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'Invalid format for Message-ID. The text should be something like "<uuid@server.com>"'));
|
2011-03-01 23:57:45 +00:00
|
|
|
}
|
|
|
|
$this->_messageId = $message;
|
|
|
|
}
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 23:57:45 +00:00
|
|
|
}
|
|
|
|
|
2012-04-20 01:38:26 +00:00
|
|
|
/**
|
|
|
|
* Domain as top level (the part after @)
|
|
|
|
*
|
|
|
|
* @param string $domain Manually set the domain for CLI mailing
|
2012-05-13 00:43:31 +00:00
|
|
|
* @return string|CakeEmail
|
2012-04-20 01:38:26 +00:00
|
|
|
*/
|
|
|
|
public function domain($domain = null) {
|
|
|
|
if ($domain === null) {
|
|
|
|
return $this->_domain;
|
|
|
|
}
|
|
|
|
$this->_domain = $domain;
|
|
|
|
return $this;
|
|
|
|
}
|
2012-04-21 00:52:10 +00:00
|
|
|
|
2011-03-01 21:51:12 +00:00
|
|
|
/**
|
2011-12-26 17:57:51 +00:00
|
|
|
* Add attachments to the email message
|
|
|
|
*
|
|
|
|
* Attachments can be defined in a few forms depending on how much control you need:
|
|
|
|
*
|
|
|
|
* Attach a single file:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $email->attachments('path/to/file');
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* Attach a file with a different filename:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $email->attachments(array('custom_name.txt' => 'path/to/file.txt'));
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* Attach a file and specify additional properties:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $email->attachments(array('custom_name.png' => array(
|
|
|
|
* 'file' => 'path/to/file',
|
|
|
|
* 'mimetype' => 'image/png',
|
2012-07-22 01:35:45 +00:00
|
|
|
* 'contentId' => 'abc123',
|
|
|
|
* 'contentDisposition' => false
|
2011-12-26 17:57:51 +00:00
|
|
|
* ));
|
|
|
|
* }}}
|
2013-09-25 17:40:14 +00:00
|
|
|
*
|
2013-06-27 22:46:34 +00:00
|
|
|
* Attach a file from string and specify additional properties:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $email->attachments(array('custom_name.png' => array(
|
|
|
|
* 'data' => file_get_contents('path/to/file'),
|
|
|
|
* 'mimetype' => 'image/png'
|
|
|
|
* ));
|
|
|
|
* }}}
|
2011-12-26 17:57:51 +00:00
|
|
|
*
|
|
|
|
* The `contentId` key allows you to specify an inline attachment. In your email text, you
|
|
|
|
* can use `<img src="cid:abc123" />` to display the image inline.
|
2011-03-01 21:51:12 +00:00
|
|
|
*
|
2012-07-22 01:35:45 +00:00
|
|
|
* The `contentDisposition` key allows you to disable the `Content-Disposition` header, this can improve
|
|
|
|
* attachment compatibility with outlook email clients.
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $attachments String with the filename or array with filenames
|
|
|
|
* @return array|CakeEmail Either the array of attachments when getting or $this when setting.
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-03-01 21:51:12 +00:00
|
|
|
*/
|
2011-03-21 12:30:16 +00:00
|
|
|
public function attachments($attachments = null) {
|
|
|
|
if ($attachments === null) {
|
|
|
|
return $this->_attachments;
|
|
|
|
}
|
2011-03-04 03:20:55 +00:00
|
|
|
$attach = array();
|
2011-04-21 01:51:30 +00:00
|
|
|
foreach ((array)$attachments as $name => $fileInfo) {
|
|
|
|
if (!is_array($fileInfo)) {
|
|
|
|
$fileInfo = array('file' => $fileInfo);
|
|
|
|
}
|
|
|
|
if (!isset($fileInfo['file'])) {
|
2013-06-27 22:46:34 +00:00
|
|
|
if (!isset($fileInfo['data'])) {
|
|
|
|
throw new SocketException(__d('cake_dev', 'No file or data specified.'));
|
|
|
|
}
|
|
|
|
if (is_int($name)) {
|
|
|
|
throw new SocketException(__d('cake_dev', 'No filename specified.'));
|
|
|
|
}
|
|
|
|
$fileInfo['data'] = chunk_split(base64_encode($fileInfo['data']), 76, "\r\n");
|
|
|
|
} else {
|
|
|
|
$fileInfo['file'] = realpath($fileInfo['file']);
|
|
|
|
if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
|
|
|
|
throw new SocketException(__d('cake_dev', 'File not found: "%s"', $fileInfo['file']));
|
|
|
|
}
|
|
|
|
if (is_int($name)) {
|
|
|
|
$name = basename($fileInfo['file']);
|
|
|
|
}
|
2011-04-21 01:51:30 +00:00
|
|
|
}
|
|
|
|
if (!isset($fileInfo['mimetype'])) {
|
|
|
|
$fileInfo['mimetype'] = 'application/octet-stream';
|
2011-03-04 03:20:55 +00:00
|
|
|
}
|
2011-04-21 01:51:30 +00:00
|
|
|
$attach[$name] = $fileInfo;
|
2011-03-01 21:51:12 +00:00
|
|
|
}
|
2011-03-04 03:20:55 +00:00
|
|
|
$this->_attachments = $attach;
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 21:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add attachments
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $attachments String with the filename or array with filenames
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail $this
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2012-07-22 01:35:45 +00:00
|
|
|
* @see CakeEmail::attachments()
|
2011-03-01 21:51:12 +00:00
|
|
|
*/
|
|
|
|
public function addAttachments($attachments) {
|
|
|
|
$current = $this->_attachments;
|
2011-03-21 12:30:16 +00:00
|
|
|
$this->attachments($attachments);
|
2011-03-04 03:20:55 +00:00
|
|
|
$this->_attachments = array_merge($current, $this->_attachments);
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-03-01 21:51:12 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 21:17:49 +00:00
|
|
|
/**
|
|
|
|
* Get generated message (used by transport classes)
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string $type Use MESSAGE_* constants or null to return the full message as array
|
|
|
|
* @return string|array String if have type, array if type is null
|
2011-03-02 21:17:49 +00:00
|
|
|
*/
|
2011-04-17 21:53:51 +00:00
|
|
|
public function message($type = null) {
|
|
|
|
switch ($type) {
|
|
|
|
case self::MESSAGE_HTML:
|
|
|
|
return $this->_htmlMessage;
|
|
|
|
case self::MESSAGE_TEXT:
|
|
|
|
return $this->_textMessage;
|
|
|
|
}
|
2011-03-02 21:17:49 +00:00
|
|
|
return $this->_message;
|
|
|
|
}
|
2011-03-01 21:51:12 +00:00
|
|
|
|
2011-04-10 01:56:09 +00:00
|
|
|
/**
|
|
|
|
* Configuration to use when send email
|
|
|
|
*
|
2013-05-23 13:45:50 +00:00
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* Load configuration from `app/Config/email.php`:
|
|
|
|
*
|
|
|
|
* `$email->config('default');`
|
|
|
|
*
|
|
|
|
* Merge an array of configuration into the instance:
|
|
|
|
*
|
|
|
|
* `$email->config(array('to' => 'bill@example.com'));`
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $config String with configuration name (from email.php), array with config or null to return current config
|
|
|
|
* @return string|array|CakeEmail
|
2011-04-10 01:56:09 +00:00
|
|
|
*/
|
|
|
|
public function config($config = null) {
|
2011-04-17 15:53:12 +00:00
|
|
|
if ($config === null) {
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_config;
|
|
|
|
}
|
2011-08-25 14:03:39 +00:00
|
|
|
if (!is_array($config)) {
|
|
|
|
$config = (string)$config;
|
2011-04-13 04:04:37 +00:00
|
|
|
}
|
|
|
|
|
2011-08-25 14:50:30 +00:00
|
|
|
$this->_applyConfig($config);
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-04-10 01:56:09 +00:00
|
|
|
}
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
|
|
|
* Send an email using the specified content, template and layout
|
2012-07-18 01:55:29 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $content String with message or array with messages
|
2011-08-26 00:31:18 +00:00
|
|
|
* @return array
|
2011-04-29 01:25:18 +00:00
|
|
|
* @throws SocketException
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-03-02 21:17:49 +00:00
|
|
|
public function send($content = null) {
|
|
|
|
if (empty($this->_from)) {
|
2011-10-15 01:01:17 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'From is not specified.'));
|
2011-03-02 21:17:49 +00:00
|
|
|
}
|
|
|
|
if (empty($this->_to) && empty($this->_cc) && empty($this->_bcc)) {
|
2012-02-19 16:47:49 +00:00
|
|
|
throw new SocketException(__d('cake_dev', 'You need to specify at least one destination for to, cc or bcc.'));
|
2011-03-02 21:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($content)) {
|
|
|
|
$content = implode("\n", $content) . "\n";
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
$this->_message = $this->_render($this->_wrap($content));
|
2011-04-19 13:22:42 +00:00
|
|
|
|
2011-08-26 00:31:18 +00:00
|
|
|
$contents = $this->transportClass()->send($this);
|
2011-08-29 01:11:30 +00:00
|
|
|
if (!empty($this->_config['log'])) {
|
2013-03-02 03:34:00 +00:00
|
|
|
$config = array(
|
|
|
|
'level' => LOG_DEBUG,
|
|
|
|
'scope' => 'email'
|
|
|
|
);
|
2011-08-26 07:33:49 +00:00
|
|
|
if ($this->_config['log'] !== true) {
|
2013-02-15 00:46:10 +00:00
|
|
|
if (!is_array($this->_config['log'])) {
|
|
|
|
$this->_config['log'] = array('level' => $this->_config['log']);
|
|
|
|
}
|
2013-03-02 03:34:00 +00:00
|
|
|
$config = array_merge($config, $this->_config['log']);
|
2011-08-26 07:33:49 +00:00
|
|
|
}
|
2013-03-02 03:34:00 +00:00
|
|
|
CakeLog::write(
|
|
|
|
$config['level'],
|
|
|
|
PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message'],
|
|
|
|
$config['scope']
|
|
|
|
);
|
2011-08-26 00:31:18 +00:00
|
|
|
}
|
|
|
|
return $contents;
|
2011-02-27 18:25:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-18 01:23:48 +00:00
|
|
|
/**
|
|
|
|
* Static method to fast create an instance of CakeEmail
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $to Address to send (see CakeEmail::to()). If null, will try to use 'to' from transport config
|
|
|
|
* @param string $subject String of subject or null to use 'subject' from transport config
|
|
|
|
* @param string|array $message String with message or array with variables to be used in render
|
|
|
|
* @param string|array $transportConfig String to use config from EmailConfig or array with configs
|
2011-04-18 01:23:48 +00:00
|
|
|
* @param boolean $send Send the email or just return the instance pre-configured
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail Instance of CakeEmail
|
2011-07-31 20:55:52 +00:00
|
|
|
* @throws SocketException
|
2011-04-18 01:23:48 +00:00
|
|
|
*/
|
2011-04-19 21:07:38 +00:00
|
|
|
public static function deliver($to = null, $subject = null, $message = null, $transportConfig = 'fast', $send = true) {
|
2011-04-18 01:23:48 +00:00
|
|
|
$class = __CLASS__;
|
2011-08-25 14:03:39 +00:00
|
|
|
$instance = new $class($transportConfig);
|
2011-04-18 01:23:48 +00:00
|
|
|
if ($to !== null) {
|
|
|
|
$instance->to($to);
|
|
|
|
}
|
|
|
|
if ($subject !== null) {
|
|
|
|
$instance->subject($subject);
|
|
|
|
}
|
|
|
|
if (is_array($message)) {
|
|
|
|
$instance->viewVars($message);
|
|
|
|
$message = null;
|
2011-08-29 15:13:41 +00:00
|
|
|
} elseif ($message === null && array_key_exists('message', $config = $instance->config())) {
|
2011-04-18 01:23:48 +00:00
|
|
|
$message = $config['message'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($send === true) {
|
|
|
|
$instance->send($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the config to an instance
|
|
|
|
*
|
|
|
|
* @param array $config
|
|
|
|
* @return void
|
2012-03-04 16:18:20 +00:00
|
|
|
* @throws ConfigureException When configuration file cannot be found, or is missing
|
|
|
|
* the named config.
|
2011-04-18 01:23:48 +00:00
|
|
|
*/
|
2011-08-25 14:50:30 +00:00
|
|
|
protected function _applyConfig($config) {
|
2011-08-17 21:11:49 +00:00
|
|
|
if (is_string($config)) {
|
2013-09-28 02:22:23 +00:00
|
|
|
if (!class_exists($this->_configClass) && !config('email')) {
|
2011-10-16 20:11:59 +00:00
|
|
|
throw new ConfigureException(__d('cake_dev', '%s not found.', APP . 'Config' . DS . 'email.php'));
|
2011-08-17 21:11:49 +00:00
|
|
|
}
|
2013-09-28 02:22:23 +00:00
|
|
|
$configs = new $this->_configClass();
|
2011-08-17 21:11:49 +00:00
|
|
|
if (!isset($configs->{$config})) {
|
2011-10-16 20:11:59 +00:00
|
|
|
throw new ConfigureException(__d('cake_dev', 'Unknown email configuration "%s".', $config));
|
2011-08-17 21:11:49 +00:00
|
|
|
}
|
|
|
|
$config = $configs->{$config};
|
|
|
|
}
|
2013-05-23 13:47:24 +00:00
|
|
|
$this->_config = array_merge($this->_config, $config);
|
2011-10-16 20:11:59 +00:00
|
|
|
if (!empty($config['charset'])) {
|
|
|
|
$this->charset = $config['charset'];
|
|
|
|
}
|
|
|
|
if (!empty($config['headerCharset'])) {
|
|
|
|
$this->headerCharset = $config['headerCharset'];
|
|
|
|
}
|
|
|
|
if (empty($this->headerCharset)) {
|
|
|
|
$this->headerCharset = $this->charset;
|
|
|
|
}
|
2011-04-18 01:23:48 +00:00
|
|
|
$simpleMethods = array(
|
2011-04-20 00:18:02 +00:00
|
|
|
'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
|
2012-04-20 01:38:26 +00:00
|
|
|
'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
|
2013-06-30 15:03:18 +00:00
|
|
|
'transport', 'emailFormat', 'theme', 'helpers', 'emailPattern'
|
2011-04-18 01:23:48 +00:00
|
|
|
);
|
|
|
|
foreach ($simpleMethods as $method) {
|
|
|
|
if (isset($config[$method])) {
|
2011-08-25 14:50:30 +00:00
|
|
|
$this->$method($config[$method]);
|
2011-04-18 01:23:48 +00:00
|
|
|
unset($config[$method]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($config['headers'])) {
|
2011-08-25 14:50:30 +00:00
|
|
|
$this->setHeaders($config['headers']);
|
2011-04-18 01:23:48 +00:00
|
|
|
unset($config['headers']);
|
|
|
|
}
|
|
|
|
if (array_key_exists('template', $config)) {
|
|
|
|
$layout = false;
|
|
|
|
if (array_key_exists('layout', $config)) {
|
|
|
|
$layout = $config['layout'];
|
|
|
|
unset($config['layout']);
|
|
|
|
}
|
2011-08-25 14:50:30 +00:00
|
|
|
$this->template($config['template'], $layout);
|
2011-04-18 01:23:48 +00:00
|
|
|
unset($config['template']);
|
|
|
|
}
|
2011-08-25 14:50:30 +00:00
|
|
|
$this->transportClass()->config($config);
|
2011-04-18 01:23:48 +00:00
|
|
|
}
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
2013-05-22 19:32:07 +00:00
|
|
|
* Reset all CakeEmail internal variables to be able to send out a new email.
|
2011-02-27 18:25:23 +00:00
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return CakeEmail $this
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
|
|
|
public function reset() {
|
2011-03-01 17:25:25 +00:00
|
|
|
$this->_to = array();
|
|
|
|
$this->_from = array();
|
2011-04-20 00:18:02 +00:00
|
|
|
$this->_sender = array();
|
2011-03-01 17:25:25 +00:00
|
|
|
$this->_replyTo = array();
|
|
|
|
$this->_readReceipt = array();
|
|
|
|
$this->_returnPath = array();
|
|
|
|
$this->_cc = array();
|
|
|
|
$this->_bcc = array();
|
2011-03-01 23:57:45 +00:00
|
|
|
$this->_messageId = true;
|
2011-03-01 17:25:25 +00:00
|
|
|
$this->_subject = '';
|
|
|
|
$this->_headers = array();
|
2011-03-01 21:25:57 +00:00
|
|
|
$this->_layout = 'default';
|
|
|
|
$this->_template = '';
|
2011-03-04 11:29:38 +00:00
|
|
|
$this->_viewRender = 'View';
|
2011-04-17 21:22:20 +00:00
|
|
|
$this->_viewVars = array();
|
2012-03-28 05:22:55 +00:00
|
|
|
$this->_theme = null;
|
2011-10-02 02:55:04 +00:00
|
|
|
$this->_helpers = array('Html');
|
2011-03-02 21:17:49 +00:00
|
|
|
$this->_textMessage = '';
|
|
|
|
$this->_htmlMessage = '';
|
|
|
|
$this->_message = '';
|
2011-03-01 21:25:57 +00:00
|
|
|
$this->_emailFormat = 'text';
|
2011-06-17 22:21:31 +00:00
|
|
|
$this->_transportName = 'Mail';
|
2011-04-13 04:04:37 +00:00
|
|
|
$this->_transportClass = null;
|
2012-04-01 23:42:18 +00:00
|
|
|
$this->charset = 'utf-8';
|
|
|
|
$this->headerCharset = null;
|
2011-03-01 21:51:12 +00:00
|
|
|
$this->_attachments = array();
|
2011-08-25 14:03:39 +00:00
|
|
|
$this->_config = array();
|
2013-06-30 15:03:18 +00:00
|
|
|
$this->_emailPattern = null;
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this;
|
2011-02-27 18:25:23 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 22:30:01 +00:00
|
|
|
/**
|
|
|
|
* Encode the specified string using the current charset
|
|
|
|
*
|
|
|
|
* @param string $text String to encode
|
|
|
|
* @return string Encoded string
|
|
|
|
*/
|
|
|
|
protected function _encode($text) {
|
|
|
|
$internalEncoding = function_exists('mb_internal_encoding');
|
|
|
|
if ($internalEncoding) {
|
|
|
|
$restore = mb_internal_encoding();
|
2011-10-16 20:11:59 +00:00
|
|
|
mb_internal_encoding($this->_appCharset);
|
2011-03-01 22:30:01 +00:00
|
|
|
}
|
2012-03-16 12:54:30 +00:00
|
|
|
if (empty($this->headerCharset)) {
|
|
|
|
$this->headerCharset = $this->charset;
|
|
|
|
}
|
2011-10-16 20:11:59 +00:00
|
|
|
$return = mb_encode_mimeheader($text, $this->headerCharset, 'B');
|
2011-03-01 22:30:01 +00:00
|
|
|
if ($internalEncoding) {
|
|
|
|
mb_internal_encoding($restore);
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2011-10-16 20:11:59 +00:00
|
|
|
/**
|
|
|
|
* Translates a string for one charset to another if the App.encoding value
|
|
|
|
* differs and the mb_convert_encoding function exists
|
|
|
|
*
|
|
|
|
* @param string $text The text to be converted
|
|
|
|
* @param string $charset the target encoding
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _encodeString($text, $charset) {
|
|
|
|
if ($this->_appCharset === $charset || !function_exists('mb_convert_encoding')) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
return mb_convert_encoding($text, $charset, $this->_appCharset);
|
|
|
|
}
|
|
|
|
|
2011-03-02 21:17:49 +00:00
|
|
|
/**
|
|
|
|
* Wrap the message to follow the RFC 2822 - 2.1.1
|
|
|
|
*
|
|
|
|
* @param string $message Message to wrap
|
|
|
|
* @return array Wrapped message
|
|
|
|
*/
|
2012-12-18 01:54:38 +00:00
|
|
|
protected function _wrap($message, $wrapLength = CakeEmail::LINE_LENGTH_MUST) {
|
2013-10-08 20:29:24 +00:00
|
|
|
if (strlen($message) === 0) {
|
2013-10-08 16:28:26 +00:00
|
|
|
return array('');
|
|
|
|
}
|
2011-03-02 21:17:49 +00:00
|
|
|
$message = str_replace(array("\r\n", "\r"), "\n", $message);
|
|
|
|
$lines = explode("\n", $message);
|
|
|
|
$formatted = array();
|
2013-06-10 21:53:25 +00:00
|
|
|
$cut = ($wrapLength == CakeEmail::LINE_LENGTH_MUST);
|
2011-03-02 21:17:49 +00:00
|
|
|
|
|
|
|
foreach ($lines as $line) {
|
2011-03-03 16:30:23 +00:00
|
|
|
if (empty($line)) {
|
|
|
|
$formatted[] = '';
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-21 01:50:46 +00:00
|
|
|
if (strlen($line) < $wrapLength) {
|
|
|
|
$formatted[] = $line;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!preg_match('/<[a-z]+.*>/i', $line)) {
|
2012-12-18 01:54:38 +00:00
|
|
|
$formatted = array_merge(
|
|
|
|
$formatted,
|
2013-05-23 16:34:39 +00:00
|
|
|
explode("\n", wordwrap($line, $wrapLength, "\n", $cut))
|
2012-12-18 01:54:38 +00:00
|
|
|
);
|
2011-03-03 16:30:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tagOpen = false;
|
|
|
|
$tmpLine = $tag = '';
|
|
|
|
$tmpLineLength = 0;
|
|
|
|
for ($i = 0, $count = strlen($line); $i < $count; $i++) {
|
|
|
|
$char = $line[$i];
|
|
|
|
if ($tagOpen) {
|
|
|
|
$tag .= $char;
|
|
|
|
if ($char === '>') {
|
|
|
|
$tagLength = strlen($tag);
|
2012-12-18 01:54:38 +00:00
|
|
|
if ($tagLength + $tmpLineLength < $wrapLength) {
|
2011-03-03 16:30:23 +00:00
|
|
|
$tmpLine .= $tag;
|
|
|
|
$tmpLineLength += $tagLength;
|
|
|
|
} else {
|
|
|
|
if ($tmpLineLength > 0) {
|
2013-05-23 16:34:39 +00:00
|
|
|
$formatted = array_merge(
|
|
|
|
$formatted,
|
|
|
|
explode("\n", wordwrap(trim($tmpLine), $wrapLength, "\n", $cut))
|
|
|
|
);
|
2011-03-03 16:30:23 +00:00
|
|
|
$tmpLine = '';
|
|
|
|
$tmpLineLength = 0;
|
|
|
|
}
|
2012-12-18 01:54:38 +00:00
|
|
|
if ($tagLength > $wrapLength) {
|
2011-03-03 16:30:23 +00:00
|
|
|
$formatted[] = $tag;
|
|
|
|
} else {
|
|
|
|
$tmpLine = $tag;
|
|
|
|
$tmpLineLength = $tagLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$tag = '';
|
|
|
|
$tagOpen = false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($char === '<') {
|
|
|
|
$tagOpen = true;
|
|
|
|
$tag = '<';
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-18 01:54:38 +00:00
|
|
|
if ($char === ' ' && $tmpLineLength >= $wrapLength) {
|
2011-03-03 16:30:23 +00:00
|
|
|
$formatted[] = $tmpLine;
|
|
|
|
$tmpLineLength = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$tmpLine .= $char;
|
|
|
|
$tmpLineLength++;
|
2012-12-18 01:54:38 +00:00
|
|
|
if ($tmpLineLength === $wrapLength) {
|
2011-03-03 16:30:23 +00:00
|
|
|
$nextChar = $line[$i + 1];
|
|
|
|
if ($nextChar === ' ' || $nextChar === '<') {
|
|
|
|
$formatted[] = trim($tmpLine);
|
|
|
|
$tmpLine = '';
|
|
|
|
$tmpLineLength = 0;
|
|
|
|
if ($nextChar === ' ') {
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$lastSpace = strrpos($tmpLine, ' ');
|
|
|
|
if ($lastSpace === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$formatted[] = trim(substr($tmpLine, 0, $lastSpace));
|
|
|
|
$tmpLine = substr($tmpLine, $lastSpace + 1);
|
2011-12-27 00:54:30 +00:00
|
|
|
|
2011-03-03 16:30:23 +00:00
|
|
|
$tmpLineLength = strlen($tmpLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($tmpLine)) {
|
|
|
|
$formatted[] = $tmpLine;
|
|
|
|
}
|
2011-03-02 21:17:49 +00:00
|
|
|
}
|
|
|
|
$formatted[] = '';
|
|
|
|
return $formatted;
|
|
|
|
}
|
|
|
|
|
2011-03-04 02:05:30 +00:00
|
|
|
/**
|
|
|
|
* Create unique boundary identifier
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-04-19 13:22:42 +00:00
|
|
|
protected function _createBoundary() {
|
|
|
|
if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
|
|
|
|
$this->_boundary = md5(uniqid(time()));
|
|
|
|
}
|
2011-03-04 02:05:30 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 04:27:18 +00:00
|
|
|
/**
|
2011-12-27 03:07:52 +00:00
|
|
|
* Attach non-embedded files by adding file contents inside boundaries.
|
2011-03-04 04:27:18 +00:00
|
|
|
*
|
2012-07-18 01:55:29 +00:00
|
|
|
* @param string $boundary Boundary to use. If null, will default to $this->_boundary
|
2011-12-27 03:07:52 +00:00
|
|
|
* @return array An array of lines to add to the message
|
2011-03-04 04:27:18 +00:00
|
|
|
*/
|
2012-01-21 01:17:02 +00:00
|
|
|
protected function _attachFiles($boundary = null) {
|
|
|
|
if ($boundary === null) {
|
|
|
|
$boundary = $this->_boundary;
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
$msg = array();
|
2011-04-21 01:51:30 +00:00
|
|
|
foreach ($this->_attachments as $filename => $fileInfo) {
|
2011-12-27 03:07:52 +00:00
|
|
|
if (!empty($fileInfo['contentId'])) {
|
|
|
|
continue;
|
2011-04-21 01:51:30 +00:00
|
|
|
}
|
2013-06-27 22:46:34 +00:00
|
|
|
$data = isset($fileInfo['data']) ? $fileInfo['data'] : $this->_readFile($fileInfo['file']);
|
2011-12-27 03:07:52 +00:00
|
|
|
|
2012-01-21 01:17:02 +00:00
|
|
|
$msg[] = '--' . $boundary;
|
2011-12-27 03:07:52 +00:00
|
|
|
$msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: base64';
|
2012-07-22 01:22:04 +00:00
|
|
|
if (
|
|
|
|
!isset($fileInfo['contentDisposition']) ||
|
|
|
|
$fileInfo['contentDisposition']
|
|
|
|
) {
|
2012-07-17 17:18:35 +00:00
|
|
|
$msg[] = 'Content-Disposition: attachment; filename="' . $filename . '"';
|
|
|
|
}
|
2011-12-27 03:07:52 +00:00
|
|
|
$msg[] = '';
|
|
|
|
$msg[] = $data;
|
|
|
|
$msg[] = '';
|
2011-03-04 04:27:18 +00:00
|
|
|
}
|
2011-12-27 03:07:52 +00:00
|
|
|
return $msg;
|
2011-03-04 04:27:18 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 02:05:30 +00:00
|
|
|
/**
|
2011-12-27 03:07:52 +00:00
|
|
|
* Read the file contents and return a base64 version of the file contents.
|
2011-03-04 02:05:30 +00:00
|
|
|
*
|
2013-05-18 02:01:18 +00:00
|
|
|
* @param string $path The absolute path to the file to read.
|
2011-12-27 03:07:52 +00:00
|
|
|
* @return string File contents in base64 encoding
|
2011-03-04 02:05:30 +00:00
|
|
|
*/
|
2013-05-18 02:01:18 +00:00
|
|
|
protected function _readFile($path) {
|
|
|
|
$File = new File($path);
|
2013-05-20 05:00:42 +00:00
|
|
|
return chunk_split(base64_encode($File->read()));
|
2011-12-27 03:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach inline/embedded files to the message.
|
|
|
|
*
|
2012-07-18 01:55:29 +00:00
|
|
|
* @param string $boundary Boundary to use. If null, will default to $this->_boundary
|
2011-12-27 03:07:52 +00:00
|
|
|
* @return array An array of lines to add to the message
|
|
|
|
*/
|
2012-01-21 01:17:02 +00:00
|
|
|
protected function _attachInlineFiles($boundary = null) {
|
|
|
|
if ($boundary === null) {
|
|
|
|
$boundary = $this->_boundary;
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
$msg = array();
|
|
|
|
foreach ($this->_attachments as $filename => $fileInfo) {
|
|
|
|
if (empty($fileInfo['contentId'])) {
|
|
|
|
continue;
|
2011-03-04 02:05:30 +00:00
|
|
|
}
|
2013-06-27 22:46:34 +00:00
|
|
|
$data = isset($fileInfo['data']) ? $fileInfo['data'] : $this->_readFile($fileInfo['file']);
|
2011-10-19 15:18:42 +00:00
|
|
|
|
2012-01-21 01:17:02 +00:00
|
|
|
$msg[] = '--' . $boundary;
|
2011-12-27 03:07:52 +00:00
|
|
|
$msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: base64';
|
|
|
|
$msg[] = 'Content-ID: <' . $fileInfo['contentId'] . '>';
|
|
|
|
$msg[] = 'Content-Disposition: inline; filename="' . $filename . '"';
|
|
|
|
$msg[] = '';
|
|
|
|
$msg[] = $data;
|
|
|
|
$msg[] = '';
|
2011-10-19 15:18:42 +00:00
|
|
|
}
|
2011-12-27 03:07:52 +00:00
|
|
|
return $msg;
|
2011-03-04 02:05:30 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 02:43:55 +00:00
|
|
|
/**
|
2011-12-27 03:07:52 +00:00
|
|
|
* Render the body of the email.
|
2011-03-04 02:43:55 +00:00
|
|
|
*
|
2013-03-18 16:17:16 +00:00
|
|
|
* @param array $content Content to render
|
2011-12-27 03:07:52 +00:00
|
|
|
* @return array Email body ready to be sent
|
2011-03-04 02:43:55 +00:00
|
|
|
*/
|
2011-03-21 12:35:17 +00:00
|
|
|
protected function _render($content) {
|
2013-06-29 14:23:55 +00:00
|
|
|
$this->_textMessage = $this->_htmlMessage = '';
|
|
|
|
|
2011-03-04 02:43:55 +00:00
|
|
|
$content = implode("\n", $content);
|
2011-12-27 00:54:30 +00:00
|
|
|
$rendered = $this->_renderTemplates($content);
|
2011-03-04 02:43:55 +00:00
|
|
|
|
2013-06-26 00:20:31 +00:00
|
|
|
$this->_createBoundary();
|
2011-12-27 00:54:30 +00:00
|
|
|
$msg = array();
|
2011-03-04 02:43:55 +00:00
|
|
|
|
2012-03-11 01:57:18 +00:00
|
|
|
$contentIds = array_filter((array)Hash::extract($this->_attachments, '{s}.contentId'));
|
2011-12-27 03:07:52 +00:00
|
|
|
$hasInlineAttachments = count($contentIds) > 0;
|
|
|
|
$hasAttachments = !empty($this->_attachments);
|
|
|
|
$hasMultipleTypes = count($rendered) > 1;
|
2011-03-04 02:43:55 +00:00
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
$boundary = $relBoundary = $textBoundary = $this->_boundary;
|
2011-04-21 03:47:21 +00:00
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
if ($hasInlineAttachments) {
|
|
|
|
$msg[] = '--' . $boundary;
|
|
|
|
$msg[] = 'Content-Type: multipart/related; boundary="rel-' . $boundary . '"';
|
2011-03-04 02:43:55 +00:00
|
|
|
$msg[] = '';
|
2012-01-12 01:44:42 +00:00
|
|
|
$relBoundary = $textBoundary = 'rel-' . $boundary;
|
2011-12-27 03:07:52 +00:00
|
|
|
}
|
2011-03-04 02:43:55 +00:00
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
if ($hasMultipleTypes) {
|
|
|
|
$msg[] = '--' . $relBoundary;
|
|
|
|
$msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $boundary . '"';
|
|
|
|
$msg[] = '';
|
|
|
|
$textBoundary = 'alt-' . $boundary;
|
2011-03-04 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
if (isset($rendered['text'])) {
|
|
|
|
if ($textBoundary !== $boundary || $hasAttachments) {
|
|
|
|
$msg[] = '--' . $textBoundary;
|
2012-05-21 10:21:02 +00:00
|
|
|
$msg[] = 'Content-Type: text/plain; charset=' . $this->_getContentTypeCharset();
|
2011-10-22 04:24:26 +00:00
|
|
|
$msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
|
2011-03-04 02:43:55 +00:00
|
|
|
$msg[] = '';
|
2011-12-27 03:07:52 +00:00
|
|
|
}
|
|
|
|
$this->_textMessage = $rendered['text'];
|
|
|
|
$content = explode("\n", $this->_textMessage);
|
|
|
|
$msg = array_merge($msg, $content);
|
|
|
|
$msg[] = '';
|
|
|
|
}
|
2012-03-04 16:18:20 +00:00
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
if (isset($rendered['html'])) {
|
|
|
|
if ($textBoundary !== $boundary || $hasAttachments) {
|
|
|
|
$msg[] = '--' . $textBoundary;
|
2012-05-21 10:21:02 +00:00
|
|
|
$msg[] = 'Content-Type: text/html; charset=' . $this->_getContentTypeCharset();
|
2011-10-22 04:24:26 +00:00
|
|
|
$msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
|
2011-03-04 02:43:55 +00:00
|
|
|
$msg[] = '';
|
|
|
|
}
|
2011-12-27 03:07:52 +00:00
|
|
|
$this->_htmlMessage = $rendered['html'];
|
|
|
|
$content = explode("\n", $this->_htmlMessage);
|
|
|
|
$msg = array_merge($msg, $content);
|
|
|
|
$msg[] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($hasMultipleTypes) {
|
|
|
|
$msg[] = '--' . $textBoundary . '--';
|
|
|
|
$msg[] = '';
|
2011-03-04 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
if ($hasInlineAttachments) {
|
2012-01-21 01:17:02 +00:00
|
|
|
$attachments = $this->_attachInlineFiles($relBoundary);
|
2011-12-27 03:07:52 +00:00
|
|
|
$msg = array_merge($msg, $attachments);
|
|
|
|
$msg[] = '';
|
|
|
|
$msg[] = '--' . $relBoundary . '--';
|
|
|
|
$msg[] = '';
|
|
|
|
}
|
2011-03-04 02:43:55 +00:00
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
if ($hasAttachments) {
|
2012-01-21 01:17:02 +00:00
|
|
|
$attachments = $this->_attachFiles($boundary);
|
2011-12-27 03:07:52 +00:00
|
|
|
$msg = array_merge($msg, $attachments);
|
2011-12-28 04:38:21 +00:00
|
|
|
}
|
|
|
|
if ($hasAttachments || $hasMultipleTypes) {
|
2011-12-27 03:07:52 +00:00
|
|
|
$msg[] = '';
|
|
|
|
$msg[] = '--' . $boundary . '--';
|
|
|
|
$msg[] = '';
|
2011-03-04 02:43:55 +00:00
|
|
|
}
|
2011-12-27 03:07:52 +00:00
|
|
|
return $msg;
|
|
|
|
}
|
2011-03-04 02:43:55 +00:00
|
|
|
|
2011-12-27 03:07:52 +00:00
|
|
|
/**
|
|
|
|
* Gets the text body types that are in this email message
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* @return array Array of types. Valid types are 'text' and 'html'
|
2011-12-27 03:07:52 +00:00
|
|
|
*/
|
|
|
|
protected function _getTypes() {
|
|
|
|
$types = array($this->_emailFormat);
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($this->_emailFormat === 'both') {
|
2011-12-27 03:07:52 +00:00
|
|
|
$types = array('html', 'text');
|
|
|
|
}
|
|
|
|
return $types;
|
2011-03-04 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
2011-12-27 00:54:30 +00:00
|
|
|
/**
|
|
|
|
* Build and set all the view properties needed to render the templated emails.
|
2011-12-27 03:07:52 +00:00
|
|
|
* If there is no template set, the $content will be returned in a hash
|
|
|
|
* of the text content types for the email.
|
2011-12-27 00:54:30 +00:00
|
|
|
*
|
|
|
|
* @param string $content The content passed in from send() in most cases.
|
|
|
|
* @return array The rendered content with html and text keys.
|
|
|
|
*/
|
2011-12-27 03:07:52 +00:00
|
|
|
protected function _renderTemplates($content) {
|
|
|
|
$types = $this->_getTypes();
|
|
|
|
$rendered = array();
|
2011-12-27 00:54:30 +00:00
|
|
|
if (empty($this->_template)) {
|
2011-12-27 03:07:52 +00:00
|
|
|
foreach ($types as $type) {
|
|
|
|
$rendered[$type] = $this->_encodeString($content, $this->charset);
|
|
|
|
}
|
|
|
|
return $rendered;
|
2011-12-27 00:54:30 +00:00
|
|
|
}
|
|
|
|
$viewClass = $this->_viewRender;
|
|
|
|
if ($viewClass !== 'View') {
|
|
|
|
list($plugin, $viewClass) = pluginSplit($viewClass, true);
|
|
|
|
$viewClass .= 'View';
|
|
|
|
App::uses($viewClass, $plugin . 'View');
|
|
|
|
}
|
|
|
|
|
|
|
|
$View = new $viewClass(null);
|
|
|
|
$View->viewVars = $this->_viewVars;
|
|
|
|
$View->helpers = $this->_helpers;
|
2013-09-02 12:32:04 +00:00
|
|
|
$View->loadHelpers();
|
2011-12-27 00:54:30 +00:00
|
|
|
|
|
|
|
list($templatePlugin, $template) = pluginSplit($this->_template);
|
|
|
|
list($layoutPlugin, $layout) = pluginSplit($this->_layout);
|
|
|
|
if ($templatePlugin) {
|
|
|
|
$View->plugin = $templatePlugin;
|
|
|
|
} elseif ($layoutPlugin) {
|
|
|
|
$View->plugin = $layoutPlugin;
|
|
|
|
}
|
2012-03-28 05:22:55 +00:00
|
|
|
if ($this->_theme) {
|
|
|
|
$View->theme = $this->_theme;
|
|
|
|
}
|
2013-08-13 18:01:33 +00:00
|
|
|
// Convert null to false, as View needs false to disable
|
|
|
|
// the layout.
|
|
|
|
if ($layout === null) {
|
|
|
|
$layout = false;
|
|
|
|
}
|
2011-12-27 00:54:30 +00:00
|
|
|
|
2013-10-08 20:29:24 +00:00
|
|
|
if ($View->get('content') === null) {
|
2011-12-27 00:54:30 +00:00
|
|
|
$View->set('content', $content);
|
2013-10-08 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($types as $type) {
|
2011-12-27 00:54:30 +00:00
|
|
|
$View->hasRendered = false;
|
|
|
|
$View->viewPath = $View->layoutPath = 'Emails' . DS . $type;
|
2012-03-04 16:18:20 +00:00
|
|
|
|
2011-12-27 00:54:30 +00:00
|
|
|
$render = $View->render($template, $layout);
|
|
|
|
$render = str_replace(array("\r\n", "\r"), "\n", $render);
|
2011-12-27 03:07:52 +00:00
|
|
|
$rendered[$type] = $this->_encodeString($render, $this->charset);
|
2011-12-27 00:54:30 +00:00
|
|
|
}
|
2013-04-29 14:18:00 +00:00
|
|
|
|
|
|
|
foreach ($rendered as $type => $content) {
|
|
|
|
$rendered[$type] = $this->_wrap($content);
|
|
|
|
$rendered[$type] = implode("\n", $rendered[$type]);
|
|
|
|
$rendered[$type] = rtrim($rendered[$type], "\n");
|
|
|
|
}
|
2011-12-27 00:54:30 +00:00
|
|
|
return $rendered;
|
|
|
|
}
|
|
|
|
|
2011-10-19 05:02:31 +00:00
|
|
|
/**
|
|
|
|
* Return the Content-Transfer Encoding value based on the set charset
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-22 04:24:26 +00:00
|
|
|
protected function _getContentTransferEncoding() {
|
2011-10-19 05:02:31 +00:00
|
|
|
$charset = strtoupper($this->charset);
|
|
|
|
if (in_array($charset, $this->_charset8bit)) {
|
|
|
|
return '8bit';
|
|
|
|
}
|
|
|
|
return '7bit';
|
|
|
|
}
|
2012-03-04 16:18:20 +00:00
|
|
|
|
2012-05-21 10:21:02 +00:00
|
|
|
/**
|
2012-05-22 00:23:49 +00:00
|
|
|
* Return charset value for Content-Type.
|
|
|
|
*
|
|
|
|
* Checks fallback/compatibility types which include workarounds
|
|
|
|
* for legacy japanese character sets.
|
2012-05-21 10:21:02 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _getContentTypeCharset() {
|
|
|
|
$charset = strtoupper($this->charset);
|
|
|
|
if (array_key_exists($charset, $this->_contentTypeCharset)) {
|
|
|
|
return strtoupper($this->_contentTypeCharset[$charset]);
|
|
|
|
}
|
2013-07-02 23:14:41 +00:00
|
|
|
return strtoupper($this->charset);
|
2012-05-21 10:21:02 +00:00
|
|
|
}
|
2012-05-22 00:23:49 +00:00
|
|
|
|
|
|
|
}
|