2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* Email Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
|
|
|
* @since CakePHP(tm) v 1.2.0.3467
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-26 01:27:02 +00:00
|
|
|
App::import('Core', 'Multibyte');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-06-03 17:26:30 +00:00
|
|
|
* EmailComponent
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-06-03 17:26:30 +00:00
|
|
|
* This component is used for handling Internet Message Format based
|
|
|
|
* based on the standard outlined in http://www.rfc-editor.org/rfc/rfc2822.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1283/Email
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
*/
|
2010-07-04 19:12:42 +00:00
|
|
|
class EmailComponent extends Component {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Recipient of the email
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $to = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The mail which the email is sent from
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $from = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The email the recipient will reply to
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $replyTo = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The read receipt email
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $readReceipt = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +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
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $return = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +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
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $cc = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +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
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $bcc = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The subject of the email
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $subject = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Associative array of a user defined headers
|
2008-06-03 17:26:30 +00:00
|
|
|
* Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $headers = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of additional headers
|
|
|
|
*
|
|
|
|
* These will NOT be used if you are using safemode and mail()
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $additionalParams = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Layout for the View
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $layout = 'default';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Template for the view
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $template = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-03 17:26:30 +00:00
|
|
|
/**
|
|
|
|
* as per RFC2822 Section 2.1.1
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $lineLength = 70;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-03 17:26:30 +00:00
|
|
|
/**
|
|
|
|
* @deprecated see lineLength
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_lineLength = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* What format should the email be sent in
|
|
|
|
*
|
|
|
|
* Supported formats:
|
|
|
|
* - text
|
|
|
|
* - html
|
|
|
|
* - both
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $sendAs = 'text';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* What method should the email be sent by
|
|
|
|
*
|
|
|
|
* Supported methods:
|
|
|
|
* - mail
|
|
|
|
* - smtp
|
|
|
|
* - debug
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $delivery = 'mail';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* charset the email is sent in
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $charset = 'utf-8';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of files that should be attached to the email.
|
|
|
|
*
|
|
|
|
* Can be both absolute and relative paths
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $attachments = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* What mailer should EmailComponent identify itself as
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $xMailer = 'CakePHP Email Component';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The list of paths to search if an attachment isnt absolute
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $filePaths = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of options to use for smtp mail method
|
|
|
|
*
|
|
|
|
* Options is:
|
|
|
|
* - port
|
|
|
|
* - host
|
|
|
|
* - timeout
|
|
|
|
* - username
|
|
|
|
* - password
|
2009-09-23 03:10:35 +00:00
|
|
|
* - client
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1290/Sending-A-Message-Using-SMTP
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-06-26 16:29:20 +00:00
|
|
|
public $smtpOptions = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Placeholder for any errors that might happen with the
|
|
|
|
* smtp mail methods
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $smtpError = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-12-16 20:47:12 +00:00
|
|
|
* Contains the rendered plain text message if one was sent.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2009-12-16 20:47:12 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $textMessage = null;
|
2009-12-16 20:47:12 +00:00
|
|
|
|
|
|
|
/**
|
2009-12-16 20:52:14 +00:00
|
|
|
* Contains the rendered HTML message if one was sent.
|
2009-12-16 20:47:12 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $htmlMessage = null;
|
2009-12-16 20:47:12 +00:00
|
|
|
|
2010-01-24 22:29:53 +00:00
|
|
|
/**
|
|
|
|
* Whether to generate a Message-ID header for the
|
|
|
|
* e-mail. True to generate a Message-ID, False to let
|
|
|
|
* it be handled by sendmail (or similar) or a string
|
|
|
|
* to completely override the Message-ID.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $messageId = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-05 17:02:22 +00:00
|
|
|
* Temporary store of message header lines
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-08-05 17:02:22 +00:00
|
|
|
* @var array
|
2010-06-25 04:32:20 +00:00
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-06-25 04:32:20 +00:00
|
|
|
protected $_header = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-05 17:02:22 +00:00
|
|
|
* If set, boundary to use for multipart mime messages
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2010-06-25 04:32:20 +00:00
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-06-25 04:32:20 +00:00
|
|
|
protected $_boundary = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-05 17:02:22 +00:00
|
|
|
* Temporary store of message lines
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-08-05 17:02:22 +00:00
|
|
|
* @var array
|
2010-06-25 04:32:20 +00:00
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-06-25 04:32:20 +00:00
|
|
|
protected $_message = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Variable that holds SMTP connection
|
|
|
|
*
|
|
|
|
* @var resource
|
2010-06-25 04:32:20 +00:00
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-06-25 04:32:20 +00:00
|
|
|
protected $_smtpConnection = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-15 03:02:38 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
|
|
|
|
* @param array $settings Array of configuration settings.
|
|
|
|
*/
|
|
|
|
public function __construct(ComponentCollection $collection, $settings = array()) {
|
|
|
|
$this->Controller = $collection->getController();
|
|
|
|
parent::__construct($collection, $settings);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-01-02 22:50:28 +00:00
|
|
|
* Initialize component
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param object $controller Instantiating controller
|
|
|
|
*/
|
2010-07-04 21:09:44 +00:00
|
|
|
public function initialize(&$controller) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (Configure::read('App.encoding') !== null) {
|
|
|
|
$this->charset = Configure::read('App.encoding');
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-01-02 22:50:28 +00:00
|
|
|
/**
|
|
|
|
* Startup component
|
|
|
|
*
|
|
|
|
* @param object $controller Instantiating controller
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function startup(&$controller) {}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Send an email using the specified content, template and layout
|
|
|
|
*
|
|
|
|
* @param mixed $content Either an array of text lines, or a string with contents
|
2010-04-28 02:50:34 +00:00
|
|
|
* If you are rendering a template this variable will be sent to the templates as `$content`
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $template Template to use when sending email
|
|
|
|
* @param string $layout Layout to use to enclose email body
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function send($content = null, $template = null, $layout = null) {
|
2009-03-22 04:19:20 +00:00
|
|
|
$this->_createHeader();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($template) {
|
|
|
|
$this->template = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($layout) {
|
|
|
|
$this->layout = $layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($content)) {
|
2008-08-05 17:02:22 +00:00
|
|
|
$content = implode("\n", $content) . "\n";
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-12-16 20:47:12 +00:00
|
|
|
$this->htmlMessage = $this->textMessage = null;
|
|
|
|
if ($content) {
|
|
|
|
if ($this->sendAs === 'html') {
|
|
|
|
$this->htmlMessage = $content;
|
|
|
|
} elseif ($this->sendAs === 'text') {
|
|
|
|
$this->textMessage = $content;
|
|
|
|
} else {
|
|
|
|
$this->htmlMessage = $this->textMessage = $content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 03:37:25 +00:00
|
|
|
if ($this->sendAs === 'text') {
|
|
|
|
$message = $this->_wrap($content);
|
|
|
|
} else {
|
|
|
|
$message = $this->_wrap($content, 998);
|
|
|
|
}
|
2009-08-10 02:20:22 +00:00
|
|
|
|
2008-08-05 17:02:22 +00:00
|
|
|
if ($this->template === null) {
|
2009-03-22 04:19:20 +00:00
|
|
|
$message = $this->_formatMessage($message);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-08-10 01:30:09 +00:00
|
|
|
$message = $this->_render($message);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-08-10 02:20:22 +00:00
|
|
|
|
2008-08-05 17:02:22 +00:00
|
|
|
$message[] = '';
|
2010-06-25 04:32:20 +00:00
|
|
|
$this->_message = $message;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!empty($this->attachments)) {
|
2009-03-22 04:19:20 +00:00
|
|
|
$this->_attachFiles();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 04:32:20 +00:00
|
|
|
if (!is_null($this->_boundary)) {
|
|
|
|
$this->_message[] = '';
|
|
|
|
$this->_message[] = '--' . $this->_boundary . '--';
|
|
|
|
$this->_message[] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 23:19:10 +00:00
|
|
|
|
2009-03-22 04:19:20 +00:00
|
|
|
$_method = '_' . $this->delivery;
|
|
|
|
$sent = $this->$_method();
|
2008-06-09 23:35:38 +00:00
|
|
|
|
2010-06-25 04:32:20 +00:00
|
|
|
$this->_header = array();
|
|
|
|
$this->_message = array();
|
2008-06-09 23:35:38 +00:00
|
|
|
|
2008-06-06 18:31:11 +00:00
|
|
|
return $sent;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Reset all EmailComponent internal variables to be able to send out a new email.
|
|
|
|
*
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1285/Sending-Multiple-Emails-in-a-loop
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function reset() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->template = null;
|
2010-02-04 02:13:11 +00:00
|
|
|
$this->to = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->from = null;
|
|
|
|
$this->replyTo = null;
|
|
|
|
$this->return = null;
|
|
|
|
$this->cc = array();
|
|
|
|
$this->bcc = array();
|
|
|
|
$this->subject = null;
|
|
|
|
$this->additionalParams = null;
|
2009-04-25 00:01:57 +00:00
|
|
|
$this->smtpError = null;
|
2009-07-10 00:07:51 +00:00
|
|
|
$this->attachments = array();
|
2009-12-16 20:47:12 +00:00
|
|
|
$this->htmlMessage = null;
|
|
|
|
$this->textMessage = null;
|
2010-01-24 22:29:53 +00:00
|
|
|
$this->messageId = true;
|
2010-06-25 04:32:20 +00:00
|
|
|
$this->_header = array();
|
|
|
|
$this->_boundary = null;
|
|
|
|
$this->_message = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Render the contents using the current layout and template.
|
|
|
|
*
|
|
|
|
* @param string $content Content to render
|
2008-08-05 17:02:22 +00:00
|
|
|
* @return array Email ready to be sent
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2009-08-10 01:30:09 +00:00
|
|
|
function _render($content) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$viewClass = $this->Controller->view;
|
|
|
|
|
|
|
|
if ($viewClass != 'View') {
|
2009-11-16 00:55:20 +00:00
|
|
|
list($plugin, $viewClass) = pluginSplit($viewClass);
|
2008-05-30 11:40:08 +00:00
|
|
|
$viewClass = $viewClass . 'View';
|
|
|
|
App::import('View', $this->Controller->view);
|
|
|
|
}
|
2009-08-10 01:30:09 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$View = new $viewClass($this->Controller, false);
|
|
|
|
$View->layout = $this->layout;
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg = array();
|
|
|
|
|
|
|
|
$content = implode("\n", $content);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($this->sendAs === 'both') {
|
|
|
|
$htmlContent = $content;
|
|
|
|
if (!empty($this->attachments)) {
|
2010-06-25 04:32:20 +00:00
|
|
|
$msg[] = '--' . $this->_boundary;
|
|
|
|
$msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg[] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-06-25 04:32:20 +00:00
|
|
|
$msg[] = '--alt-' . $this->_boundary;
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$msg[] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$content = $View->element('email' . DS . 'text' . DS . $this->template, array('content' => $content), true);
|
|
|
|
$View->layoutPath = 'email' . DS . 'text';
|
2009-12-16 20:47:12 +00:00
|
|
|
$content = explode("\n", $this->textMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
|
|
|
|
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg = array_merge($msg, $content);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg[] = '';
|
2010-06-25 04:32:20 +00:00
|
|
|
$msg[] = '--alt-' . $this->_boundary;
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$msg[] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-08-05 17:02:22 +00:00
|
|
|
$htmlContent = $View->element('email' . DS . 'html' . DS . $this->template, array('content' => $htmlContent), true);
|
2008-05-30 11:40:08 +00:00
|
|
|
$View->layoutPath = 'email' . DS . 'html';
|
2009-12-16 20:47:12 +00:00
|
|
|
$htmlContent = explode("\n", $this->htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg = array_merge($msg, $htmlContent);
|
|
|
|
$msg[] = '';
|
2010-06-25 04:32:20 +00:00
|
|
|
$msg[] = '--alt-' . $this->_boundary . '--';
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg[] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-08-05 17:02:22 +00:00
|
|
|
return $msg;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->attachments)) {
|
|
|
|
if ($this->sendAs === 'html') {
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg[] = '';
|
2010-06-25 04:32:20 +00:00
|
|
|
$msg[] = '--' . $this->_boundary;
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$msg[] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-06-25 04:32:20 +00:00
|
|
|
$msg[] = '--' . $this->_boundary;
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
|
|
|
|
$msg[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$msg[] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $View->element('email' . DS . $this->sendAs . DS . $this->template, array('content' => $content), true);
|
|
|
|
$View->layoutPath = 'email' . DS . $this->sendAs;
|
2009-12-16 20:47:12 +00:00
|
|
|
$content = explode("\n", $rendered = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
|
|
|
|
|
|
|
|
if ($this->sendAs === 'html') {
|
|
|
|
$this->htmlMessage = $rendered;
|
|
|
|
} else {
|
|
|
|
$this->textMessage = $rendered;
|
|
|
|
}
|
|
|
|
|
2008-08-05 17:02:22 +00:00
|
|
|
$msg = array_merge($msg, $content);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
return $msg;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Create unique boundary identifier
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _createboundary() {
|
2010-06-25 04:32:20 +00:00
|
|
|
$this->_boundary = md5(uniqid(time()));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-25 23:17:17 +00:00
|
|
|
/**
|
|
|
|
* Sets headers for the message
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array Associative array containing headers to be set.
|
|
|
|
*/
|
2010-02-07 21:45:36 +00:00
|
|
|
function header($headers) {
|
|
|
|
foreach ($headers as $header => $value) {
|
2010-06-25 04:32:20 +00:00
|
|
|
$this->_header[] = sprintf('%s: %s', trim($header), trim($value));
|
2010-02-07 21:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Create emails headers including (but not limited to) from email address, reply to,
|
|
|
|
* bcc and cc.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _createHeader() {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers = array();
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->delivery == 'smtp') {
|
2010-02-04 02:13:11 +00:00
|
|
|
if (is_array($this->to)) {
|
2010-02-07 21:45:36 +00:00
|
|
|
$headers['To'] = implode(', ', array_map(array($this, '_formatAddress'), $this->to));
|
2010-02-04 02:13:11 +00:00
|
|
|
} else {
|
2010-02-07 21:45:36 +00:00
|
|
|
$headers['To'] = $this->_formatAddress($this->to);
|
2010-02-04 02:13:11 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-02-07 21:45:36 +00:00
|
|
|
$headers['From'] = $this->_formatAddress($this->from);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!empty($this->replyTo)) {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['Reply-To'] = $this->_formatAddress($this->replyTo);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($this->return)) {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['Return-Path'] = $this->_formatAddress($this->return);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($this->readReceipt)) {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['Disposition-Notification-To'] = $this->_formatAddress($this->readReceipt);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->cc)) {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['cc'] = implode(', ', array_map(array($this, '_formatAddress'), $this->cc));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-10-20 17:09:42 +00:00
|
|
|
if (!empty($this->bcc) && $this->delivery != 'smtp') {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['Bcc'] = implode(', ', array_map(array($this, '_formatAddress'), $this->bcc));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($this->delivery == 'smtp') {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['Subject'] = $this->_encode($this->subject);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-02 19:02:23 +00:00
|
|
|
|
2010-01-24 22:29:53 +00:00
|
|
|
if ($this->messageId !== false) {
|
|
|
|
if ($this->messageId === true) {
|
|
|
|
$headers['Message-ID'] = '<' . String::UUID() . '@' . env('HTTP_HOST') . '>';
|
|
|
|
} else {
|
2010-04-02 19:02:23 +00:00
|
|
|
$headers['Message-ID'] = $this->messageId;
|
2010-01-24 22:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-02 19:02:23 +00:00
|
|
|
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['X-Mailer'] = $this->xMailer;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!empty($this->headers)) {
|
|
|
|
foreach ($this->headers as $key => $val) {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['X-' . $key] = $val;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->attachments)) {
|
2009-03-22 04:19:20 +00:00
|
|
|
$this->_createBoundary();
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['MIME-Version'] = '1.0';
|
2010-06-25 04:32:20 +00:00
|
|
|
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
|
2009-03-25 23:17:17 +00:00
|
|
|
$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.';
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif ($this->sendAs === 'text') {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif ($this->sendAs === 'html') {
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['Content-Type'] = 'text/html; charset=' . $this->charset;
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif ($this->sendAs === 'both') {
|
2010-06-25 04:32:20 +00:00
|
|
|
$headers['Content-Type'] = 'multipart/alternative; boundary="alt-' . $this->_boundary . '"';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-25 23:17:17 +00:00
|
|
|
$headers['Content-Transfer-Encoding'] = '7bit';
|
|
|
|
|
|
|
|
$this->header($headers);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Format the message by seeing if it has attachments.
|
|
|
|
*
|
|
|
|
* @param string $message Message to format
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _formatMessage($message) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($this->attachments)) {
|
2010-06-25 04:32:20 +00:00
|
|
|
$prefix = array('--' . $this->_boundary);
|
2009-07-21 21:54:05 +00:00
|
|
|
if ($this->sendAs === 'text') {
|
|
|
|
$prefix[] = 'Content-Type: text/plain; charset=' . $this->charset;
|
|
|
|
} elseif ($this->sendAs === 'html') {
|
|
|
|
$prefix[] = 'Content-Type: text/html; charset=' . $this->charset;
|
|
|
|
} elseif ($this->sendAs === 'both') {
|
2010-06-25 04:32:20 +00:00
|
|
|
$prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
|
2009-07-21 21:54:05 +00:00
|
|
|
}
|
|
|
|
$prefix[] = 'Content-Transfer-Encoding: 7bit';
|
|
|
|
$prefix[] = '';
|
2008-08-05 17:02:22 +00:00
|
|
|
$message = array_merge($prefix, $message);
|
|
|
|
}
|
|
|
|
return $message;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Attach files by adding file contents inside boundaries.
|
|
|
|
*
|
|
|
|
* @access private
|
2008-08-05 17:02:22 +00:00
|
|
|
* @TODO: modify to use the core File class?
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _attachFiles() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$files = array();
|
2010-04-02 19:02:23 +00:00
|
|
|
foreach ($this->attachments as $filename => $attachment) {
|
2009-03-22 04:19:20 +00:00
|
|
|
$file = $this->_findFiles($attachment);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($file)) {
|
2010-04-02 19:02:23 +00:00
|
|
|
if (is_int($filename)) {
|
|
|
|
$filename = basename($file);
|
|
|
|
}
|
|
|
|
$files[$filename] = $file;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 19:02:23 +00:00
|
|
|
foreach ($files as $filename => $file) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$handle = fopen($file, 'rb');
|
|
|
|
$data = fread($handle, filesize($file));
|
|
|
|
$data = chunk_split(base64_encode($data)) ;
|
|
|
|
fclose($handle);
|
|
|
|
|
2010-06-25 04:32:20 +00:00
|
|
|
$this->_message[] = '--' . $this->_boundary;
|
|
|
|
$this->_message[] = 'Content-Type: application/octet-stream';
|
|
|
|
$this->_message[] = 'Content-Transfer-Encoding: base64';
|
|
|
|
$this->_message[] = 'Content-Disposition: attachment; filename="' . basename($filename) . '"';
|
|
|
|
$this->_message[] = '';
|
|
|
|
$this->_message[] = $data;
|
|
|
|
$this->_message[] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Find the specified attachment in the list of file paths
|
|
|
|
*
|
|
|
|
* @param string $attachment Attachment file name to find
|
|
|
|
* @return string Path to located file
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _findFiles($attachment) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (file_exists($attachment)) {
|
|
|
|
return $attachment;
|
|
|
|
}
|
|
|
|
foreach ($this->filePaths as $path) {
|
|
|
|
if (file_exists($path . DS . $attachment)) {
|
|
|
|
$file = $path . DS . $attachment;
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-06-03 17:26:30 +00:00
|
|
|
* Wrap the message using EmailComponent::$lineLength
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $message Message to wrap
|
2010-06-21 03:37:25 +00:00
|
|
|
* @param integer $lineLength Max length of line
|
2008-08-05 17:02:22 +00:00
|
|
|
* @return array Wrapped message
|
2010-06-21 03:37:25 +00:00
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-06-21 03:37:25 +00:00
|
|
|
function _wrap($message, $lineLength = null) {
|
2009-03-22 04:19:20 +00:00
|
|
|
$message = $this->_strip($message, true);
|
2008-05-30 11:40:08 +00:00
|
|
|
$message = str_replace(array("\r\n","\r"), "\n", $message);
|
|
|
|
$lines = explode("\n", $message);
|
2008-08-05 17:02:22 +00:00
|
|
|
$formatted = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-06-03 17:26:30 +00:00
|
|
|
if ($this->_lineLength !== null) {
|
2010-04-15 15:52:49 +00:00
|
|
|
trigger_error(__('_lineLength cannot be accessed please use lineLength'), E_USER_WARNING);
|
2008-06-03 17:26:30 +00:00
|
|
|
$this->lineLength = $this->_lineLength;
|
|
|
|
}
|
|
|
|
|
2010-06-21 03:37:25 +00:00
|
|
|
if (!$lineLength) {
|
|
|
|
$lineLength = $this->lineLength;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($lines as $line) {
|
2008-10-23 00:10:44 +00:00
|
|
|
if (substr($line, 0, 1) == '.') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$line = '.' . $line;
|
|
|
|
}
|
2010-06-21 03:37:25 +00:00
|
|
|
$formatted = array_merge($formatted, explode("\n", wordwrap($line, $lineLength, "\n", true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-05 17:02:22 +00:00
|
|
|
$formatted[] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
return $formatted;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Encode the specified string using the current charset
|
|
|
|
*
|
|
|
|
* @param string $subject String to encode
|
|
|
|
* @return string Encoded string
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _encode($subject) {
|
|
|
|
$subject = $this->_strip($subject);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-01 18:36:38 +00:00
|
|
|
$nl = "\r\n";
|
|
|
|
if ($this->delivery == 'mail') {
|
|
|
|
$nl = '';
|
2008-08-05 17:02:22 +00:00
|
|
|
}
|
2010-07-19 00:48:15 +00:00
|
|
|
$internalEncoding = function_exists('mb_internal_encoding');
|
|
|
|
if ($internalEncoding) {
|
|
|
|
$restore = mb_internal_encoding();
|
|
|
|
mb_internal_encoding($this->charset);
|
|
|
|
}
|
|
|
|
$return = mb_encode_mimeheader($subject, $this->charset, 'B', $nl);
|
|
|
|
if ($internalEncoding) {
|
|
|
|
mb_internal_encoding($restore);
|
|
|
|
}
|
|
|
|
return $return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Format a string as an email address
|
|
|
|
*
|
|
|
|
* @param string $string String representing an email address
|
|
|
|
* @return string Email address suitable for email headers or smtp pipe
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _formatAddress($string, $smtp = false) {
|
2010-09-12 15:48:09 +00:00
|
|
|
$hasAlias = preg_match('/((.*)\s)?<(.+)>/', $string, $matches);
|
|
|
|
if ($smtp && $hasAlias) {
|
|
|
|
return $this->_strip(' <' . $matches[3] . '>');
|
|
|
|
} elseif ($smtp) {
|
|
|
|
return $this->_strip(' <' . $string . '>');
|
|
|
|
}
|
|
|
|
if ($hasAlias && !empty($matches[2])) {
|
|
|
|
return $this->_strip($matches[2] . ' <' . $matches[3] . '>');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-03-22 04:19:20 +00:00
|
|
|
return $this->_strip($string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-09-02 15:16:10 +00:00
|
|
|
* Remove certain elements (such as bcc:, to:, %0a) from given value.
|
|
|
|
* Helps prevent header injection / mainipulation on user content.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $value Value to strip
|
|
|
|
* @param boolean $message Set to true to indicate main message content
|
|
|
|
* @return string Stripped value
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _strip($value, $message = false) {
|
2009-07-02 02:58:43 +00:00
|
|
|
$search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:';
|
|
|
|
$search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*';
|
|
|
|
|
2008-10-01 18:36:38 +00:00
|
|
|
if ($message !== true) {
|
2008-11-11 19:54:15 +00:00
|
|
|
$search .= '|\r|\n';
|
2008-10-01 18:36:38 +00:00
|
|
|
}
|
|
|
|
$search = '#(?:' . $search . ')#i';
|
|
|
|
while (preg_match($search, $value)) {
|
|
|
|
$value = preg_replace($search, '', $value);
|
|
|
|
}
|
|
|
|
return $value;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Wrapper for PHP mail function used for sending out emails
|
|
|
|
*
|
|
|
|
* @return bool Success
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _mail() {
|
2010-09-25 03:27:22 +00:00
|
|
|
$header = implode("\r\n", $this->_header);
|
|
|
|
$message = implode("\r\n", $this->_message);
|
2010-02-04 02:13:11 +00:00
|
|
|
if (is_array($this->to)) {
|
2010-02-07 21:45:36 +00:00
|
|
|
$to = implode(', ', array_map(array($this, '_formatAddress'), $this->to));
|
2010-02-04 02:13:11 +00:00
|
|
|
} else {
|
|
|
|
$to = $this->to;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if (ini_get('safe_mode')) {
|
2010-02-07 21:45:36 +00:00
|
|
|
return @mail($to, $this->_encode($this->subject), $message, $header);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-02-07 21:45:36 +00:00
|
|
|
return @mail($to, $this->_encode($this->subject), $message, $header, $this->additionalParams);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sends out email via SMTP
|
|
|
|
*
|
|
|
|
* @return bool Success
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _smtp() {
|
2009-03-15 20:52:27 +00:00
|
|
|
App::import('Core', array('CakeSocket'));
|
2008-06-03 17:26:30 +00:00
|
|
|
|
2010-06-02 21:14:58 +00:00
|
|
|
$defaults = array(
|
|
|
|
'host' => 'localhost',
|
|
|
|
'port' => 25,
|
|
|
|
'protocol' => 'smtp',
|
|
|
|
'timeout' => 30
|
|
|
|
);
|
2010-06-03 17:46:20 +00:00
|
|
|
$this->smtpOptions = array_merge($defaults, $this->smtpOptions);
|
2010-06-29 02:31:35 +00:00
|
|
|
$this->_smtpConnection = new CakeSocket($this->smtpOptions);
|
2008-06-03 17:26:30 +00:00
|
|
|
|
2010-06-25 04:32:20 +00:00
|
|
|
if (!$this->_smtpConnection->connect()) {
|
|
|
|
$this->smtpError = $this->_smtpConnection->lastError();
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
2009-03-22 04:19:20 +00:00
|
|
|
} elseif (!$this->_smtpSend(null, '220')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-06-03 17:26:30 +00:00
|
|
|
|
2009-09-06 23:49:50 +00:00
|
|
|
$httpHost = env('HTTP_HOST');
|
|
|
|
|
2009-06-11 03:07:38 +00:00
|
|
|
if (isset($this->smtpOptions['client'])) {
|
|
|
|
$host = $this->smtpOptions['client'];
|
2009-09-06 23:49:50 +00:00
|
|
|
} elseif (!empty($httpHost)) {
|
|
|
|
$host = $httpHost;
|
2009-04-24 18:20:51 +00:00
|
|
|
} else {
|
2009-09-06 23:49:50 +00:00
|
|
|
$host = 'localhost';
|
2009-04-24 18:20:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-09 22:22:06 +00:00
|
|
|
if (!$this->_smtpSend("EHLO {$host}", '250') && !$this->_smtpSend("HELO {$host}", '250')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->smtpOptions['username']) && isset($this->smtpOptions['password'])) {
|
2009-03-22 04:19:20 +00:00
|
|
|
$authRequired = $this->_smtpSend('AUTH LOGIN', '334|503');
|
2008-10-08 17:53:48 +00:00
|
|
|
if ($authRequired == '334') {
|
2009-03-22 04:19:20 +00:00
|
|
|
if (!$this->_smtpSend(base64_encode($this->smtpOptions['username']), '334')) {
|
2008-10-08 17:53:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-03-22 04:19:20 +00:00
|
|
|
if (!$this->_smtpSend(base64_encode($this->smtpOptions['password']), '235')) {
|
2008-10-08 17:53:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($authRequired != '503') {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-22 04:19:20 +00:00
|
|
|
if (!$this->_smtpSend('MAIL FROM: ' . $this->_formatAddress($this->from, true))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-04 02:13:11 +00:00
|
|
|
if (!is_array($this->to)) {
|
|
|
|
$tos = array($this->to);
|
|
|
|
} else {
|
|
|
|
$tos = $this->to;
|
|
|
|
}
|
|
|
|
foreach ($tos as $to) {
|
2010-02-07 21:45:36 +00:00
|
|
|
if (!$this->_smtpSend('RCPT TO: ' . $this->_formatAddress($to, true))) {
|
2010-02-04 02:13:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->cc as $cc) {
|
2009-03-22 04:19:20 +00:00
|
|
|
if (!$this->_smtpSend('RCPT TO: ' . $this->_formatAddress($cc, true))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($this->bcc as $bcc) {
|
2009-03-22 04:19:20 +00:00
|
|
|
if (!$this->_smtpSend('RCPT TO: ' . $this->_formatAddress($bcc, true))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-22 04:19:20 +00:00
|
|
|
if (!$this->_smtpSend('DATA', '354')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-25 04:32:20 +00:00
|
|
|
$header = implode("\r\n", $this->_header);
|
|
|
|
$message = implode("\r\n", $this->_message);
|
2009-03-22 04:19:20 +00:00
|
|
|
if (!$this->_smtpSend($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.")) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-03-22 04:19:20 +00:00
|
|
|
$this->_smtpSend('QUIT', false);
|
2008-06-03 17:26:30 +00:00
|
|
|
|
2010-06-25 04:32:20 +00:00
|
|
|
$this->_smtpConnection->disconnect();
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-05-25 01:24:58 +00:00
|
|
|
* Protected method for sending data to SMTP connection
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $data data to be sent to SMTP server
|
|
|
|
* @param mixed $checkCode code to check for in server response, false to skip
|
|
|
|
* @return bool Success
|
2010-05-25 01:24:58 +00:00
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _smtpSend($data, $checkCode = '250') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_null($data)) {
|
2010-06-25 04:32:20 +00:00
|
|
|
$this->_smtpConnection->write($data . "\r\n");
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-25 01:24:58 +00:00
|
|
|
while ($checkCode !== false) {
|
2010-05-29 15:04:29 +00:00
|
|
|
$response = '';
|
|
|
|
$startTime = time();
|
|
|
|
while (substr($response, -2) !== "\r\n" && ((time() - $startTime) < $this->smtpOptions['timeout'])) {
|
2010-06-29 02:31:35 +00:00
|
|
|
$response .= $this->_smtpConnection->read();
|
2010-05-29 15:04:29 +00:00
|
|
|
}
|
2010-05-31 02:29:21 +00:00
|
|
|
if (substr($response, -2) !== "\r\n") {
|
2010-05-29 15:04:29 +00:00
|
|
|
$this->smtpError = 'timeout';
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-25 01:24:58 +00:00
|
|
|
$response = end(explode("\r\n", rtrim($response, "\r\n")));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-25 01:24:58 +00:00
|
|
|
if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) {
|
|
|
|
if ($code[2] === '-') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return $code[1];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-10-08 17:53:48 +00:00
|
|
|
$this->smtpError = $response;
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Set as controller flash message a debug message showing current settings in component
|
|
|
|
*
|
|
|
|
* @return boolean Success
|
|
|
|
* @access private
|
|
|
|
*/
|
2009-03-22 04:19:20 +00:00
|
|
|
function _debug() {
|
2008-08-05 17:02:22 +00:00
|
|
|
$nl = "\n";
|
2010-06-25 04:32:20 +00:00
|
|
|
$header = implode($nl, $this->_header);
|
|
|
|
$message = implode($nl, $this->_message);
|
2008-05-30 11:40:08 +00:00
|
|
|
$fm = '<pre>';
|
|
|
|
|
2010-02-04 02:13:11 +00:00
|
|
|
if (is_array($this->to)) {
|
2010-02-07 21:45:36 +00:00
|
|
|
$to = implode(', ', array_map(array($this, '_formatAddress'), $this->to));
|
2010-02-04 02:13:11 +00:00
|
|
|
} else {
|
|
|
|
$to = $this->to;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->delivery == 'smtp') {
|
|
|
|
$fm .= sprintf('%s %s%s', 'Host:', $this->smtpOptions['host'], $nl);
|
|
|
|
$fm .= sprintf('%s %s%s', 'Port:', $this->smtpOptions['port'], $nl);
|
|
|
|
$fm .= sprintf('%s %s%s', 'Timeout:', $this->smtpOptions['timeout'], $nl);
|
|
|
|
}
|
2010-02-04 02:13:11 +00:00
|
|
|
$fm .= sprintf('%s %s%s', 'To:', $to, $nl);
|
2008-05-30 11:40:08 +00:00
|
|
|
$fm .= sprintf('%s %s%s', 'From:', $this->from, $nl);
|
2009-03-22 04:19:20 +00:00
|
|
|
$fm .= sprintf('%s %s%s', 'Subject:', $this->_encode($this->subject), $nl);
|
2008-08-05 17:02:22 +00:00
|
|
|
$fm .= sprintf('%s%3$s%3$s%s', 'Header:', $header, $nl);
|
2008-05-30 11:40:08 +00:00
|
|
|
$fm .= sprintf('%s%3$s%3$s%s', 'Parameters:', $this->additionalParams, $nl);
|
2008-08-05 17:02:22 +00:00
|
|
|
$fm .= sprintf('%s%3$s%3$s%s', 'Message:', $message, $nl);
|
2008-05-30 11:40:08 +00:00
|
|
|
$fm .= '</pre>';
|
|
|
|
|
2010-04-17 16:27:50 +00:00
|
|
|
if (isset($this->Controller->Session)) {
|
|
|
|
$this->Controller->Session->setFlash($fm, 'default', null, 'email');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return $fm;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|