2011-02-27 18:25:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Cake E-Mail
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package cake.libs
|
|
|
|
* @since CakePHP(tm) v 2.0.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2011-04-13 03:29:34 +00:00
|
|
|
App::uses('Validation', 'Utility');
|
|
|
|
App::uses('Multibyte', 'I18n');
|
|
|
|
App::uses('AbstractTransport', 'Network/Email');
|
2011-04-16 02:02:07 +00:00
|
|
|
App::uses('String', 'Utility');
|
|
|
|
App::uses('View', 'View');
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cake e-mail class.
|
|
|
|
*
|
|
|
|
* This class is used for handling Internet Message Format based
|
|
|
|
* based on the standard outlined in http://www.rfc-editor.org/rfc/rfc2822.txt
|
|
|
|
*
|
|
|
|
* @package cake.libs
|
|
|
|
*/
|
|
|
|
class CakeEmail {
|
2011-03-01 04:19:47 +00:00
|
|
|
/**
|
|
|
|
* What mailer should EmailComponent identify itself as
|
|
|
|
*
|
|
|
|
* @constant EMAIL_CLIENT
|
|
|
|
*/
|
|
|
|
const EMAIL_CLIENT = 'CakePHP Email Component';
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Line length - no must more - RFC 2822 - 2.1.1
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-03-01 04:19:47 +00:00
|
|
|
protected $_from = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The email the recipient will reply to
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-03-01 17:09:08 +00:00
|
|
|
protected $_replyTo = array();
|
2011-02-27 18:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The read receipt email
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @var mixed True to generate, False to ignore, String with value
|
|
|
|
*/
|
|
|
|
protected $_messageId = true;
|
|
|
|
|
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();
|
|
|
|
|
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-03-01 21:29:46 +00:00
|
|
|
protected $_transportName = 'mail';
|
2011-02-27 18:25:23 +00:00
|
|
|
|
2011-04-13 04:04:37 +00:00
|
|
|
/**
|
|
|
|
* Instance of tranport class
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $_transportClass = null;
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
|
|
|
* charset the email is sent in
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $charset = 'utf-8';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $_config = 'default';
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$charset = Configure::read('App.encoding');
|
|
|
|
if ($charset !== null) {
|
|
|
|
$this->charset = $charset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2011-03-01 17:09:08 +00:00
|
|
|
* @param mixed $email
|
2011-03-01 04:31:30 +00:00
|
|
|
* @param string $name
|
2011-03-21 12:18:36 +00:00
|
|
|
* @return mixed
|
2011-03-01 16:42:21 +00:00
|
|
|
* @thrown 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-04-13 03:45:02 +00:00
|
|
|
return $this->_setEmailSingle('_from', $email, $name, __d('cake', 'From requires only 1 email address.'));
|
2011-03-01 04:31:30 +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
|
|
|
*
|
|
|
|
* @param mixed $email
|
|
|
|
* @param string $name
|
2011-03-21 12:18:36 +00:00
|
|
|
* @return mixed
|
2011-03-01 17:09:08 +00:00
|
|
|
* @thrown SocketException
|
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function replyTo($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_replyTo;
|
|
|
|
}
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_setEmailSingle('_replyTo', $email, $name, __d('cake', '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
|
|
|
*
|
|
|
|
* @param mixed $email
|
|
|
|
* @param string $name
|
2011-03-21 12:18:36 +00:00
|
|
|
* @return mixed
|
2011-03-01 17:09:08 +00:00
|
|
|
* @thrown SocketException
|
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function readReceipt($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_readReceipt;
|
|
|
|
}
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_setEmailSingle('_readReceipt', $email, $name, __d('cake', '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
|
|
|
*
|
|
|
|
* @param mixed $email
|
|
|
|
* @param string $name
|
2011-03-21 12:18:36 +00:00
|
|
|
* @return mixed
|
2011-03-01 17:09:08 +00:00
|
|
|
* @thrown SocketException
|
|
|
|
*/
|
2011-03-21 12:18:36 +00:00
|
|
|
public function returnPath($email = null, $name = null) {
|
|
|
|
if ($email === null) {
|
|
|
|
return $this->_returnPath;
|
|
|
|
}
|
2011-04-13 03:45:02 +00:00
|
|
|
return $this->_setEmailSingle('_returnPath', $email, $name, __d('cake', '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
|
|
|
*
|
2011-03-21 12:18:36 +00:00
|
|
|
* @param mixed $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
|
2011-03-21 12:18:36 +00:00
|
|
|
* @return mixed
|
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
|
|
|
|
*
|
|
|
|
* @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
|
|
|
|
* @param string $name
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return object $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
|
|
|
*
|
|
|
|
* @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
|
|
|
|
* @param string $name
|
2011-03-21 12:18:36 +00:00
|
|
|
* @return mixed
|
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
|
|
|
|
*
|
|
|
|
* @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
|
|
|
|
* @param string $name
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return object $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
|
|
|
*
|
|
|
|
* @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
|
|
|
|
* @param string $name
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return mixed
|
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
|
|
|
|
*
|
|
|
|
* @param mixed $email String with email, Array with email as key, name as value or email as value (without name)
|
|
|
|
* @param string $name
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return object $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
|
|
|
}
|
|
|
|
|
2011-03-01 05:09:05 +00:00
|
|
|
/**
|
|
|
|
* Set email
|
|
|
|
*
|
|
|
|
* @param string $varName
|
|
|
|
* @param mixed $email
|
|
|
|
* @param mixed $name
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return object $this
|
2011-03-01 22:46:20 +00:00
|
|
|
* @thrown SocketException
|
2011-03-01 05:09:05 +00:00
|
|
|
*/
|
|
|
|
protected function _setEmail($varName, $email, $name) {
|
|
|
|
if (!is_array($email)) {
|
2011-03-01 22:46:20 +00:00
|
|
|
if (!Validation::email($email)) {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', '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
|
|
|
}
|
2011-03-01 22:58:04 +00:00
|
|
|
if (!Validation::email($key)) {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', '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
|
|
|
|
* @param mixed $email
|
|
|
|
* @param string $name
|
|
|
|
* @param string $throwMessage
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return object $this
|
2011-03-01 17:09:08 +00:00
|
|
|
* @thrown SocketExpceiton
|
|
|
|
*/
|
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
|
|
|
|
* @param mixed $email
|
|
|
|
* @param mixed $name
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return object $this
|
2011-03-01 05:09:05 +00:00
|
|
|
*/
|
|
|
|
protected function _addEmail($varName, $email, $name) {
|
|
|
|
if (!is_array($email)) {
|
2011-03-01 22:46:20 +00:00
|
|
|
if (!Validation::email($email)) {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', '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;
|
|
|
|
}
|
|
|
|
if (!Validation::email($key)) {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', '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
|
|
|
/**
|
|
|
|
* Set Subject
|
|
|
|
*
|
|
|
|
* @param string $subject
|
2011-03-21 12:18:36 +00:00
|
|
|
* @return mixed
|
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-03-21 12:30:16 +00:00
|
|
|
* Sets eaders for the message
|
2011-02-27 18:25:23 +00:00
|
|
|
*
|
|
|
|
* @param array Associative array containing headers to be set.
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return object $this
|
2011-03-01 04:19:47 +00:00
|
|
|
* @thrown 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-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', '$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-04-13 03:45:02 +00:00
|
|
|
* @return mixed $this
|
2011-03-01 04:19:47 +00:00
|
|
|
* @thrown SocketException
|
|
|
|
*/
|
|
|
|
public function addHeaders($headers) {
|
|
|
|
if (!is_array($headers)) {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', '$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()) {
|
|
|
|
$defaults = array(
|
|
|
|
'from' => false,
|
|
|
|
'replyTo' => false,
|
|
|
|
'readReceipt' => false,
|
|
|
|
'returnPath' => false,
|
|
|
|
'to' => false,
|
|
|
|
'cc' => false,
|
|
|
|
'bcc' => false,
|
|
|
|
'subject' => false
|
|
|
|
);
|
|
|
|
$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}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
$headers['Message-ID'] = '<' . String::UUID() . '@' . env('HTTP_HOST') . '>';
|
|
|
|
} else {
|
|
|
|
$headers['Message-ID'] = $this->_messageId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:16:50 +00:00
|
|
|
if ($include['subject']) {
|
|
|
|
$headers['Subject'] = $this->_subject;
|
|
|
|
}
|
|
|
|
|
2011-03-04 02:43:55 +00:00
|
|
|
if (!empty($this->_attachments)) {
|
2011-03-04 02:05:30 +00:00
|
|
|
$this->_createBoundary();
|
|
|
|
$headers['MIME-Version'] = '1.0';
|
|
|
|
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
|
|
|
|
$headers[] = 'This part of the E-mail should never be seen. If';
|
|
|
|
$headers[] = 'you are reading this, consider upgrading your e-mail';
|
|
|
|
$headers[] = 'client to a MIME-compatible client.';
|
|
|
|
} elseif ($this->_emailFormat === 'text') {
|
|
|
|
$headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
|
|
|
|
} elseif ($this->_emailFormat === 'html') {
|
|
|
|
$headers['Content-Type'] = 'text/html; charset=' . $this->charset;
|
|
|
|
} elseif ($this->_emailFormat === 'both') {
|
|
|
|
$headers['Content-Type'] = 'multipart/alternative; boundary="alt-' . $this->_boundary . '"';
|
|
|
|
}
|
|
|
|
$headers['Content-Transfer-Encoding'] = '7bit';
|
|
|
|
|
2011-03-01 18:16:50 +00:00
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format addresses
|
|
|
|
*
|
|
|
|
* @param array $address
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _formatAddress($address) {
|
|
|
|
$return = array();
|
|
|
|
foreach ($address as $email => $alias) {
|
|
|
|
if ($email === $alias) {
|
|
|
|
$return[] = $email;
|
|
|
|
} else {
|
2011-03-01 22:30:01 +00:00
|
|
|
$return[] = sprintf('%s <%s>', $this->_encode($alias), $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
|
|
|
*
|
2011-04-17 23:40:18 +00:00
|
|
|
* @param mixed $template Template name or null to not use
|
|
|
|
* @param mixed $layout Layout name or null to not use
|
2011-03-21 12:30:16 +00:00
|
|
|
* @return mixed
|
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
|
2011-03-21 12:30:16 +00:00
|
|
|
* @return mixed
|
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
|
2011-04-17 21:22:20 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
2011-03-21 12:30:16 +00:00
|
|
|
* @return mixed
|
2011-03-01 21:25:57 +00:00
|
|
|
* @thrown SocketException
|
|
|
|
*/
|
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-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', '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
|
2011-03-21 12:30:16 +00:00
|
|
|
* @return mixed
|
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
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* @thrown SocketException
|
|
|
|
*/
|
|
|
|
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)) {
|
|
|
|
throw new SocketException(__d('cake', 'Class "%s" not found.', $transportClassname));
|
|
|
|
} elseif (!method_exists($transportClassname, 'send')) {
|
|
|
|
throw new SocketException(__d('cake', 'The "%s" do not have send method.', $transportClassname));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param mixed $message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID
|
2011-03-21 12:30:16 +00:00
|
|
|
* @return mixed
|
2011-03-01 23:57:45 +00:00
|
|
|
* @thrown SocketException
|
|
|
|
*/
|
2011-03-21 12:30:16 +00:00
|
|
|
public function messageID($message = null) {
|
|
|
|
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)) {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', 'Invalid format to 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
|
|
|
}
|
|
|
|
|
2011-03-01 21:51:12 +00:00
|
|
|
/**
|
2011-03-21 12:30:16 +00:00
|
|
|
* Attachments
|
2011-03-01 21:51:12 +00:00
|
|
|
*
|
|
|
|
* @param mixed $attachments String with the filename or array with filenames
|
2011-03-21 12:30:16 +00:00
|
|
|
* @return mixed
|
2011-03-01 21:51:12 +00:00
|
|
|
* @thrown SocketException
|
|
|
|
*/
|
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();
|
|
|
|
foreach ((array)$attachments as $name => $file) {
|
|
|
|
$path = realpath($file);
|
2011-03-01 21:51:12 +00:00
|
|
|
if ($path === false) {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', 'File not found: "%s"', $attach));
|
2011-03-01 21:51:12 +00:00
|
|
|
}
|
2011-03-04 03:20:55 +00:00
|
|
|
if (is_int($name)) {
|
|
|
|
$name = basename($path);
|
|
|
|
}
|
|
|
|
$attach[$name] = $path;
|
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
|
|
|
|
*
|
|
|
|
* @param mixed $attachments String with the filename or array with filenames
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return object $this
|
2011-03-01 21:51:12 +00:00
|
|
|
* @thrown SocketException
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
*
|
2011-04-17 21:53:51 +00:00
|
|
|
* @param mixed $type Use MESSAGE_* constants or null to return the full message as array
|
|
|
|
* @return mixed 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
|
|
|
|
*
|
|
|
|
* @param mixed $config String with configuration name (from email.php), array with config or null to return current config
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return mixed
|
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-04-13 04:04:37 +00:00
|
|
|
|
2011-04-13 03:45:02 +00:00
|
|
|
if (is_array($config)) {
|
|
|
|
$this->_config = $config;
|
|
|
|
} else {
|
|
|
|
$this->_config = (string)$config;
|
2011-04-10 01:56:09 +00:00
|
|
|
}
|
2011-04-13 04:04:37 +00:00
|
|
|
|
|
|
|
if ($this->_transportClass) {
|
|
|
|
$this->_transportClass->config($this->_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
|
|
|
|
*
|
|
|
|
* @return boolean Success
|
2011-03-02 21:17:49 +00:00
|
|
|
* @thrown SocketExpcetion
|
2011-02-27 18:25:23 +00:00
|
|
|
*/
|
2011-03-02 21:17:49 +00:00
|
|
|
public function send($content = null) {
|
2011-04-10 01:56:09 +00:00
|
|
|
if (is_string($this->_config)) {
|
|
|
|
if (!config('email')) {
|
2011-04-17 22:23:09 +00:00
|
|
|
throw new SocketException(__d('cake', '%s not found.', CONFIGS . 'email.php'));
|
2011-04-10 01:56:09 +00:00
|
|
|
}
|
2011-04-17 22:11:46 +00:00
|
|
|
$configs = new EmailConfig();
|
2011-04-10 01:56:09 +00:00
|
|
|
if (!isset($configs->{$this->_config})) {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', 'Unknown email configuration "%s".', $this->_config));
|
2011-04-10 01:56:09 +00:00
|
|
|
}
|
|
|
|
$config = $configs->{$this->_config};
|
2011-04-17 22:23:09 +00:00
|
|
|
if (isset($config['transport'])) {
|
|
|
|
$this->transport($config['transport']);
|
|
|
|
}
|
2011-04-10 01:56:09 +00:00
|
|
|
} else {
|
|
|
|
$config = $this->_config;
|
|
|
|
}
|
|
|
|
|
2011-03-02 21:17:49 +00:00
|
|
|
if (empty($this->_from)) {
|
2011-04-10 01:56:09 +00:00
|
|
|
if (!empty($config['from'])) {
|
2011-04-17 22:23:09 +00:00
|
|
|
$this->from($config['from']);
|
2011-04-10 01:56:09 +00:00
|
|
|
} else {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketException(__d('cake', 'From is not specified.'));
|
2011-04-10 01:56:09 +00:00
|
|
|
}
|
2011-03-02 21:17:49 +00:00
|
|
|
}
|
|
|
|
if (empty($this->_to) && empty($this->_cc) && empty($this->_bcc)) {
|
2011-04-13 03:37:50 +00:00
|
|
|
throw new SocketExpcetion(__d('cake', 'You need specify one destination on to, cc or bcc.'));
|
2011-03-02 21:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($content)) {
|
|
|
|
$content = implode("\n", $content) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_textMessage = $this->_htmlMessage = '';
|
|
|
|
if ($content !== null) {
|
|
|
|
if ($this->_emailFormat === 'text') {
|
|
|
|
$this->_textMessage = $content;
|
|
|
|
} elseif ($this->_emailFormat === 'html') {
|
|
|
|
$this->_htmlMessage = $content;
|
|
|
|
} elseif ($this->_emailFormat === 'both') {
|
|
|
|
$this->_textMessage = $this->_htmlMessage = $content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = $this->_wrap($content);
|
2011-03-04 02:43:55 +00:00
|
|
|
if (empty($this->_template)) {
|
2011-03-04 02:05:30 +00:00
|
|
|
$message = $this->_formatMessage($message);
|
2011-03-02 21:17:49 +00:00
|
|
|
} else {
|
2011-03-04 02:43:55 +00:00
|
|
|
$message = $this->_render($message);
|
2011-03-02 21:17:49 +00:00
|
|
|
}
|
|
|
|
$message[] = '';
|
|
|
|
$this->_message = $message;
|
|
|
|
|
2011-03-04 02:43:55 +00:00
|
|
|
if (!empty($this->_attachments)) {
|
2011-03-04 04:27:18 +00:00
|
|
|
$this->_attachFiles();
|
2011-03-02 21:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($this->_boundary)) {
|
|
|
|
$this->_message[] = '';
|
|
|
|
$this->_message[] = '--' . $this->_boundary . '--';
|
|
|
|
$this->_message[] = '';
|
|
|
|
}
|
|
|
|
|
2011-04-13 04:04:37 +00:00
|
|
|
$transport = $this->transportClass();
|
|
|
|
$transport->config($config);
|
2011-03-02 21:17:49 +00:00
|
|
|
|
|
|
|
return $transport->send($this);
|
2011-02-27 18:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset all EmailComponent internal variables to be able to send out a new email.
|
|
|
|
*
|
2011-04-13 03:45:02 +00:00
|
|
|
* @return object $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();
|
|
|
|
$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();
|
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-03-01 21:29:46 +00:00
|
|
|
$this->_transportName = 'mail';
|
2011-04-13 04:04:37 +00:00
|
|
|
$this->_transportClass = null;
|
2011-03-01 21:51:12 +00:00
|
|
|
$this->_attachments = array();
|
2011-04-10 03:34:34 +00:00
|
|
|
$this->_config = 'default';
|
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();
|
|
|
|
mb_internal_encoding($this->charset);
|
|
|
|
}
|
|
|
|
$return = mb_encode_mimeheader($text, $this->charset, 'B');
|
|
|
|
if ($internalEncoding) {
|
|
|
|
mb_internal_encoding($restore);
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2011-03-21 12:35:17 +00:00
|
|
|
protected function _wrap($message) {
|
2011-03-02 21:17:49 +00:00
|
|
|
$message = str_replace(array("\r\n", "\r"), "\n", $message);
|
|
|
|
$lines = explode("\n", $message);
|
|
|
|
$formatted = array();
|
|
|
|
|
|
|
|
foreach ($lines as $line) {
|
2011-03-03 16:30:23 +00:00
|
|
|
if (empty($line)) {
|
|
|
|
$formatted[] = '';
|
|
|
|
continue;
|
|
|
|
}
|
2011-03-02 21:17:49 +00:00
|
|
|
if ($line[0] === '.') {
|
|
|
|
$line = '.' . $line;
|
|
|
|
}
|
2011-03-03 16:30:23 +00:00
|
|
|
if (!preg_match('/\<[a-z]/i', $line)) {
|
2011-03-04 12:48:51 +00:00
|
|
|
$formatted = array_merge($formatted, explode("\n", wordwrap($line, self::LINE_LENGTH_SHOULD, "\n")));
|
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);
|
|
|
|
if ($tagLength + $tmpLineLength < self::LINE_LENGTH_SHOULD) {
|
|
|
|
$tmpLine .= $tag;
|
|
|
|
$tmpLineLength += $tagLength;
|
|
|
|
} else {
|
|
|
|
if ($tmpLineLength > 0) {
|
|
|
|
$formatted[] = trim($tmpLine);
|
|
|
|
$tmpLine = '';
|
|
|
|
$tmpLineLength = 0;
|
|
|
|
}
|
|
|
|
if ($tagLength > self::LINE_LENGTH_SHOULD) {
|
|
|
|
$formatted[] = $tag;
|
|
|
|
} else {
|
|
|
|
$tmpLine = $tag;
|
|
|
|
$tmpLineLength = $tagLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$tag = '';
|
|
|
|
$tagOpen = false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($char === '<') {
|
|
|
|
$tagOpen = true;
|
|
|
|
$tag = '<';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($char === ' ' && $tmpLineLength >= self::LINE_LENGTH_SHOULD) {
|
|
|
|
$formatted[] = $tmpLine;
|
|
|
|
$tmpLineLength = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$tmpLine .= $char;
|
|
|
|
$tmpLineLength++;
|
|
|
|
if ($tmpLineLength === self::LINE_LENGTH_SHOULD) {
|
|
|
|
$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);
|
|
|
|
$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-03-21 12:35:17 +00:00
|
|
|
protected function _createboundary() {
|
2011-03-04 02:05:30 +00:00
|
|
|
$this->_boundary = md5(uniqid(time()));
|
|
|
|
}
|
|
|
|
|
2011-03-04 04:27:18 +00:00
|
|
|
/**
|
|
|
|
* Attach files by adding file contents inside boundaries.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-03-21 12:35:17 +00:00
|
|
|
protected function _attachFiles() {
|
2011-03-04 04:27:18 +00:00
|
|
|
foreach ($this->_attachments as $filename => $file) {
|
|
|
|
$handle = fopen($file, 'rb');
|
|
|
|
$data = fread($handle, filesize($file));
|
|
|
|
$data = chunk_split(base64_encode($data)) ;
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
$this->_message[] = '--' . $this->_boundary;
|
|
|
|
$this->_message[] = 'Content-Type: application/octet-stream';
|
|
|
|
$this->_message[] = 'Content-Transfer-Encoding: base64';
|
|
|
|
$this->_message[] = 'Content-Disposition: attachment; filename="' . $filename . '"';
|
|
|
|
$this->_message[] = '';
|
|
|
|
$this->_message[] = $data;
|
|
|
|
$this->_message[] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-04 02:05:30 +00:00
|
|
|
/**
|
|
|
|
* Format the message by seeing if it has attachments.
|
|
|
|
*
|
|
|
|
* @param array $message Message to format
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-03-21 12:35:17 +00:00
|
|
|
protected function _formatMessage($message) {
|
2011-03-04 02:05:30 +00:00
|
|
|
if (!empty($this->_attachments)) {
|
|
|
|
$prefix = array('--' . $this->_boundary);
|
|
|
|
if ($this->_emailFormat === 'text') {
|
|
|
|
$prefix[] = 'Content-Type: text/plain; charset=' . $this->charset;
|
|
|
|
} elseif ($this->_emailFormat === 'html') {
|
|
|
|
$prefix[] = 'Content-Type: text/html; charset=' . $this->charset;
|
|
|
|
} elseif ($this->_emailFormat === 'both') {
|
|
|
|
$prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
|
|
|
|
}
|
|
|
|
$prefix[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$prefix[] = '';
|
|
|
|
$message = array_merge($prefix, $message);
|
|
|
|
}
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2011-03-04 02:43:55 +00:00
|
|
|
/**
|
|
|
|
* Render the contents using the current layout and template.
|
|
|
|
*
|
|
|
|
* @param string $content Content to render
|
|
|
|
* @return array Email ready to be sent
|
|
|
|
* @access private
|
|
|
|
*/
|
2011-03-21 12:35:17 +00:00
|
|
|
protected function _render($content) {
|
2011-03-04 11:29:38 +00:00
|
|
|
$viewClass = $this->_viewRender;
|
2011-03-04 02:43:55 +00:00
|
|
|
|
|
|
|
if ($viewClass !== 'View') {
|
2011-04-13 03:29:34 +00:00
|
|
|
list($plugin, $viewClass) = pluginSplit($viewClass, true);
|
|
|
|
$viewClass .= 'View';
|
|
|
|
App::uses($viewClass, $plugin . 'View');
|
2011-03-04 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 11:29:38 +00:00
|
|
|
$View = new $viewClass(null);
|
|
|
|
$View->layout = $this->_layout;
|
2011-04-17 21:22:20 +00:00
|
|
|
$View->viewVars = $this->_viewVars;
|
2011-03-04 02:43:55 +00:00
|
|
|
$msg = array();
|
|
|
|
|
|
|
|
$content = implode("\n", $content);
|
|
|
|
|
|
|
|
if ($this->_emailFormat === 'both') {
|
|
|
|
$htmlContent = $content;
|
|
|
|
if (!empty($this->_attachments)) {
|
|
|
|
$msg[] = '--' . $this->_boundary;
|
|
|
|
$msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
|
|
|
|
$msg[] = '';
|
|
|
|
}
|
|
|
|
$msg[] = '--alt-' . $this->_boundary;
|
|
|
|
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$msg[] = '';
|
|
|
|
|
|
|
|
$content = $View->element('email' . DS . 'text' . DS . $this->_template, array('content' => $content), true);
|
|
|
|
$View->layoutPath = 'email' . DS . 'text';
|
|
|
|
$content = explode("\n", $this->_textMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
|
|
|
|
|
|
|
|
$msg = array_merge($msg, $content);
|
|
|
|
|
|
|
|
$msg[] = '';
|
|
|
|
$msg[] = '--alt-' . $this->_boundary;
|
|
|
|
$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$msg[] = '';
|
|
|
|
|
|
|
|
$htmlContent = $View->element('email' . DS . 'html' . DS . $this->_template, array('content' => $htmlContent), true);
|
|
|
|
$View->layoutPath = 'email' . DS . 'html';
|
|
|
|
$htmlContent = explode("\n", $this->_htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
|
|
|
|
$msg = array_merge($msg, $htmlContent);
|
|
|
|
$msg[] = '';
|
|
|
|
$msg[] = '--alt-' . $this->_boundary . '--';
|
|
|
|
$msg[] = '';
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->_attachments)) {
|
|
|
|
if ($this->_emailFormat === 'html') {
|
|
|
|
$msg[] = '';
|
|
|
|
$msg[] = '--' . $this->_boundary;
|
|
|
|
$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$msg[] = '';
|
|
|
|
} else {
|
|
|
|
$msg[] = '--' . $this->_boundary;
|
|
|
|
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$msg[] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $View->element('email' . DS . $this->_emailFormat . DS . $this->_template, array('content' => $content), true);
|
|
|
|
$View->layoutPath = 'email' . DS . $this->_emailFormat;
|
|
|
|
$content = explode("\n", $rendered = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
|
|
|
|
|
|
|
|
if ($this->_emailFormat === 'html') {
|
|
|
|
$this->_htmlMessage = $rendered;
|
|
|
|
} else {
|
|
|
|
$this->_textMessage = $rendered;
|
|
|
|
}
|
|
|
|
|
2011-04-13 03:32:26 +00:00
|
|
|
return array_merge($msg, $content);
|
2011-03-04 02:43:55 +00:00
|
|
|
}
|
|
|
|
|
2011-02-27 18:25:23 +00:00
|
|
|
}
|