mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 17:16:18 +00:00
Merge branch '2.0-email-component' into 2.0-email-new
This commit is contained in:
commit
1e491b0bbe
2 changed files with 215 additions and 633 deletions
|
@ -19,6 +19,7 @@
|
|||
|
||||
App::uses('Component', 'Controller');
|
||||
App::uses('Multibyte', 'I18n');
|
||||
App::uses('CakeEmail', 'Network');
|
||||
|
||||
/**
|
||||
* EmailComponent
|
||||
|
@ -28,8 +29,7 @@ App::uses('Multibyte', 'I18n');
|
|||
*
|
||||
* @package cake.libs.controller.components
|
||||
* @link http://book.cakephp.org/view/1283/Email
|
||||
* @deprecated
|
||||
* @see CakeEmail Lib
|
||||
*
|
||||
*/
|
||||
class EmailComponent extends Component {
|
||||
|
||||
|
@ -150,14 +150,6 @@ class EmailComponent extends Component {
|
|||
*/
|
||||
public $template = null;
|
||||
|
||||
/**
|
||||
* as per RFC2822 Section 2.1.1
|
||||
*
|
||||
* @var integer
|
||||
* @access public
|
||||
*/
|
||||
public $lineLength = 70;
|
||||
|
||||
/**
|
||||
* Line feed character(s) to be used when sending using mail() function
|
||||
* By default PHP_EOL is used.
|
||||
|
@ -168,12 +160,7 @@ class EmailComponent extends Component {
|
|||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $lineFeed = PHP_EOL;
|
||||
|
||||
/**
|
||||
* @deprecated see lineLength
|
||||
*/
|
||||
protected $_lineLength = null;
|
||||
public $lineFeed = PHP_EOL;
|
||||
|
||||
/**
|
||||
* What format should the email be sent in
|
||||
|
@ -283,28 +270,11 @@ class EmailComponent extends Component {
|
|||
public $messageId = true;
|
||||
|
||||
/**
|
||||
* Temporary store of message header lines
|
||||
* Controller reference
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
* @var object Controller
|
||||
*/
|
||||
protected $_header = array();
|
||||
|
||||
/**
|
||||
* If set, boundary to use for multipart mime messages
|
||||
*
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
protected $_boundary = null;
|
||||
|
||||
/**
|
||||
* Temporary store of message lines
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
protected $_message = array();
|
||||
protected $_controller = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
@ -313,7 +283,7 @@ class EmailComponent extends Component {
|
|||
* @param array $settings Array of configuration settings.
|
||||
*/
|
||||
public function __construct(ComponentCollection $collection, $settings = array()) {
|
||||
$this->Controller = $collection->getController();
|
||||
$this->_controller = $collection->getController();
|
||||
parent::__construct($collection, $settings);
|
||||
}
|
||||
|
||||
|
@ -328,13 +298,6 @@ class EmailComponent extends Component {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Startup component
|
||||
*
|
||||
* @param object $controller Instantiating controller
|
||||
*/
|
||||
public function startup($controller) {}
|
||||
|
||||
/**
|
||||
* Send an email using the specified content, template and layout
|
||||
*
|
||||
|
@ -345,60 +308,71 @@ class EmailComponent extends Component {
|
|||
* @return boolean Success
|
||||
*/
|
||||
public function send($content = null, $template = null, $layout = null) {
|
||||
$this->_createHeader();
|
||||
$lib = new CakeEmail();
|
||||
$lib->charset = $this->charset;
|
||||
|
||||
$lib->from($this->_formatAddresses((array)$this->from));
|
||||
if (!empty($this->to)) {
|
||||
$lib->to($this->_formatAddresses((array)$this->to));
|
||||
}
|
||||
if (!empty($this->cc)) {
|
||||
$lib->cc($this->_formatAddresses((array)$this->cc));
|
||||
}
|
||||
if (!empty($this->bcc)) {
|
||||
$lib->bcc($this->_formatAddresses((array)$this->bcc));
|
||||
}
|
||||
if (!empty($this->replyTo)) {
|
||||
$lib->replyTo($this->_formatAddresses((array)$this->replyTo));
|
||||
}
|
||||
if (!empty($this->return)) {
|
||||
$lib->returnPath($this->_formatAddresses((array)$this->return));
|
||||
}
|
||||
if (!empty($readReceipt)) {
|
||||
$lib->readReceipt($this->_formatAddresses((array)$this->readReceipt));
|
||||
}
|
||||
|
||||
$lib->subject($this->subject)->messageID($this->messageId);
|
||||
|
||||
$headers = array();
|
||||
foreach ($this->headers as $key => $value) {
|
||||
$headers['X-' . $key] = $value;
|
||||
}
|
||||
if ($this->date != false) {
|
||||
$headers['Date'] = $this->date;
|
||||
}
|
||||
$lib->setHeaders($headers);
|
||||
|
||||
if ($template) {
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
if ($layout) {
|
||||
$this->layout = $layout;
|
||||
}
|
||||
|
||||
if (is_array($content)) {
|
||||
$content = implode("\n", $content) . "\n";
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->sendAs === 'text') {
|
||||
$message = $this->_wrap($content);
|
||||
} else {
|
||||
$message = $this->_wrap($content, 998);
|
||||
}
|
||||
|
||||
if ($this->template === null) {
|
||||
$message = $this->_formatMessage($message);
|
||||
} else {
|
||||
$message = $this->_render($message);
|
||||
}
|
||||
|
||||
$message[] = '';
|
||||
$this->_message = $message;
|
||||
$lib->layout($this->layout, $this->template)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs);
|
||||
|
||||
if (!empty($this->attachments)) {
|
||||
$this->_attachFiles();
|
||||
$lib->attachments($this->_formatAttachFiles());
|
||||
}
|
||||
|
||||
if (!is_null($this->_boundary)) {
|
||||
$this->_message[] = '';
|
||||
$this->_message[] = '--' . $this->_boundary . '--';
|
||||
$this->_message[] = '';
|
||||
$lib->transport($this->delivery);
|
||||
if ($this->delivery === 'mail') {
|
||||
$lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams));
|
||||
} elseif ($this->delivery === 'smtp') {
|
||||
$lib->config($this->smtpOptions);
|
||||
} else {
|
||||
$lib->config(array());
|
||||
}
|
||||
|
||||
$sent = $lib->send($content);
|
||||
|
||||
$_method = '_' . $this->delivery;
|
||||
//$sent = $this->$_method();
|
||||
$sent = true;
|
||||
$this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML);
|
||||
if (empty($this->htmlMessage)) {
|
||||
$this->htmlMessage = null;
|
||||
}
|
||||
$this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT);
|
||||
if (empty($this->textMessage)) {
|
||||
$this->textMessage = null;
|
||||
}
|
||||
|
||||
$this->_header = array();
|
||||
$this->_message = array();
|
||||
|
@ -426,227 +400,14 @@ class EmailComponent extends Component {
|
|||
$this->htmlMessage = null;
|
||||
$this->textMessage = null;
|
||||
$this->messageId = true;
|
||||
$this->_header = array();
|
||||
$this->_boundary = null;
|
||||
$this->_message = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the contents using the current layout and template.
|
||||
* Format the attach array
|
||||
*
|
||||
* @param string $content Content to render
|
||||
* @return array Email ready to be sent
|
||||
* @access private
|
||||
* @return array
|
||||
*/
|
||||
function _render($content) {
|
||||
$viewClass = $this->Controller->view;
|
||||
|
||||
if ($viewClass != 'View') {
|
||||
list($plugin, $viewClass) = pluginSplit($viewClass, true);
|
||||
$viewClass = $viewClass . 'View';
|
||||
App::uses($viewClass, $plugin . 'View');
|
||||
}
|
||||
|
||||
$View = new $viewClass($this->Controller);
|
||||
$View->layout = $this->layout;
|
||||
$msg = array();
|
||||
|
||||
$content = implode("\n", $content);
|
||||
|
||||
if ($this->sendAs === '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[] = '';
|
||||
|
||||
ClassRegistry::removeObject('view');
|
||||
return $msg;
|
||||
}
|
||||
|
||||
if (!empty($this->attachments)) {
|
||||
if ($this->sendAs === '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->sendAs . DS . $this->template, array('content' => $content), true);
|
||||
$View->layoutPath = 'email' . DS . $this->sendAs;
|
||||
$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;
|
||||
}
|
||||
|
||||
$msg = array_merge($msg, $content);
|
||||
ClassRegistry::removeObject('view');
|
||||
|
||||
return $msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create unique boundary identifier
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
function _createboundary() {
|
||||
$this->_boundary = md5(uniqid(time()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets headers for the message
|
||||
*
|
||||
* @access public
|
||||
* @param array Associative array containing headers to be set.
|
||||
*/
|
||||
function header($headers) {
|
||||
foreach ($headers as $header => $value) {
|
||||
$this->_header[] = sprintf('%s: %s', trim($header), trim($value));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Create emails headers including (but not limited to) from email address, reply to,
|
||||
* bcc and cc.
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
function _createHeader() {
|
||||
$headers = array();
|
||||
|
||||
if ($this->delivery == 'smtp') {
|
||||
$headers['To'] = implode(', ', array_map(array($this, '_formatAddress'), (array)$this->to));
|
||||
}
|
||||
$headers['From'] = $this->_formatAddress($this->from);
|
||||
|
||||
if (!empty($this->replyTo)) {
|
||||
$headers['Reply-To'] = $this->_formatAddress($this->replyTo);
|
||||
}
|
||||
if (!empty($this->return)) {
|
||||
$headers['Return-Path'] = $this->_formatAddress($this->return);
|
||||
}
|
||||
if (!empty($this->readReceipt)) {
|
||||
$headers['Disposition-Notification-To'] = $this->_formatAddress($this->readReceipt);
|
||||
}
|
||||
|
||||
if (!empty($this->cc)) {
|
||||
$headers['Cc'] = implode(', ', array_map(array($this, '_formatAddress'), (array)$this->cc));
|
||||
}
|
||||
|
||||
if (!empty($this->bcc) && $this->delivery != 'smtp') {
|
||||
$headers['Bcc'] = implode(', ', array_map(array($this, '_formatAddress'), (array)$this->bcc));
|
||||
}
|
||||
if ($this->delivery == 'smtp') {
|
||||
$headers['Subject'] = $this->_encode($this->subject);
|
||||
}
|
||||
|
||||
if ($this->messageId !== false) {
|
||||
if ($this->messageId === true) {
|
||||
$headers['Message-ID'] = '<' . String::UUID() . '@' . env('HTTP_HOST') . '>';
|
||||
} else {
|
||||
$headers['Message-ID'] = $this->messageId;
|
||||
}
|
||||
}
|
||||
|
||||
$date = $this->date;
|
||||
if ($date == false) {
|
||||
$date = date(DATE_RFC2822);
|
||||
}
|
||||
$headers['Date'] = $date;
|
||||
|
||||
$headers['X-Mailer'] = $this->xMailer;
|
||||
|
||||
if (!empty($this->headers)) {
|
||||
foreach ($this->headers as $key => $val) {
|
||||
$headers['X-' . $key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($this->attachments)) {
|
||||
$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->sendAs === 'text') {
|
||||
$headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
|
||||
} elseif ($this->sendAs === 'html') {
|
||||
$headers['Content-Type'] = 'text/html; charset=' . $this->charset;
|
||||
} elseif ($this->sendAs === 'both') {
|
||||
$headers['Content-Type'] = 'multipart/alternative; boundary="alt-' . $this->_boundary . '"';
|
||||
}
|
||||
|
||||
$headers['Content-Transfer-Encoding'] = '7bit';
|
||||
|
||||
$this->header($headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the message by seeing if it has attachments.
|
||||
*
|
||||
* @param string $message Message to format
|
||||
* @access private
|
||||
*/
|
||||
function _formatMessage($message) {
|
||||
if (!empty($this->attachments)) {
|
||||
$prefix = array('--' . $this->_boundary);
|
||||
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') {
|
||||
$prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
|
||||
}
|
||||
$prefix[] = 'Content-Transfer-Encoding: 7bit';
|
||||
$prefix[] = '';
|
||||
$message = array_merge($prefix, $message);
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach files by adding file contents inside boundaries.
|
||||
*
|
||||
* @access private
|
||||
* @TODO: modify to use the core File class?
|
||||
*/
|
||||
function _attachFiles() {
|
||||
protected function _formatAttachFiles() {
|
||||
$files = array();
|
||||
foreach ($this->attachments as $filename => $attachment) {
|
||||
$file = $this->_findFiles($attachment);
|
||||
|
@ -657,21 +418,7 @@ class EmailComponent extends Component {
|
|||
$files[$filename] = $file;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($files 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="' . basename($filename) . '"';
|
||||
$this->_message[] = '';
|
||||
$this->_message[] = $data;
|
||||
$this->_message[] = '';
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -694,39 +441,6 @@ class EmailComponent extends Component {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the message using EmailComponent::$lineLength
|
||||
*
|
||||
* @param string $message Message to wrap
|
||||
* @param integer $lineLength Max length of line
|
||||
* @return array Wrapped message
|
||||
* @access protected
|
||||
*/
|
||||
function _wrap($message, $lineLength = null) {
|
||||
$message = $this->_strip($message, true);
|
||||
$message = str_replace(array("\r\n","\r"), "\n", $message);
|
||||
$lines = explode("\n", $message);
|
||||
$formatted = array();
|
||||
|
||||
if ($this->_lineLength !== null) {
|
||||
trigger_error(__d('cake_dev', '_lineLength cannot be accessed please use lineLength'), E_USER_WARNING);
|
||||
$this->lineLength = $this->_lineLength;
|
||||
}
|
||||
|
||||
if (!$lineLength) {
|
||||
$lineLength = $this->lineLength;
|
||||
}
|
||||
|
||||
foreach ($lines as $line) {
|
||||
if (substr($line, 0, 1) == '.') {
|
||||
$line = '.' . $line;
|
||||
}
|
||||
$formatted = array_merge($formatted, explode("\n", wordwrap($line, $lineLength, "\n", true)));
|
||||
}
|
||||
$formatted[] = '';
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the specified string using the current charset
|
||||
*
|
||||
|
@ -754,24 +468,22 @@ class EmailComponent extends Component {
|
|||
}
|
||||
|
||||
/**
|
||||
* Format a string as an email address
|
||||
* Format addresses to be an array with email as key and alias as value
|
||||
*
|
||||
* @param string $string String representing an email address
|
||||
* @return string Email address suitable for email headers or smtp pipe
|
||||
* @access private
|
||||
* @param array $addresses
|
||||
* @return array
|
||||
*/
|
||||
function _formatAddress($string, $smtp = false) {
|
||||
$hasAlias = preg_match('/((.*))?\s?<(.+)>/', $string, $matches);
|
||||
if ($smtp && $hasAlias) {
|
||||
return $this->_strip('<' . $matches[3] . '>');
|
||||
} elseif ($smtp) {
|
||||
return $this->_strip('<' . $string . '>');
|
||||
protected function _formatAddresses($addresses) {
|
||||
$formatted = array();
|
||||
foreach ($addresses as $address) {
|
||||
if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
|
||||
$formatted[$this->_strip($matches[3])] = $this->_encode($matches[2]);
|
||||
} else {
|
||||
$address = $this->_strip($address);
|
||||
$formatted[$address] = $address;
|
||||
}
|
||||
|
||||
if ($hasAlias && !empty($matches[2])) {
|
||||
return $this->_encode($matches[2]) . $this->_strip(' <' . $matches[3] . '>');
|
||||
}
|
||||
return $this->_strip($string);
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
*/
|
||||
App::uses('Controller', 'Controller');
|
||||
App::uses('EmailComponent', 'Controller/Component');
|
||||
App::uses('CakeSocket', 'Network');
|
||||
App::uses('AbstractTransport', 'Network/Email');
|
||||
|
||||
/**
|
||||
* EmailTestComponent class
|
||||
|
@ -29,75 +29,6 @@ App::uses('CakeSocket', 'Network');
|
|||
*/
|
||||
class EmailTestComponent extends EmailComponent {
|
||||
|
||||
/**
|
||||
* Convenience setter for testing.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function setHeaders($headers) {
|
||||
$this->_header += $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience getter for testing.
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
function getHeaders() {
|
||||
if (empty($this->_header)) {
|
||||
return array();
|
||||
}
|
||||
return $this->_header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience setter for testing.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function setBoundary() {
|
||||
$this->_createBoundary();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience getter for testing.
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function getBoundary() {
|
||||
if (empty($this->_boundary)) {
|
||||
return null;
|
||||
}
|
||||
return $this->_boundary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience getter for testing.
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function getMessage() {
|
||||
if (empty($this->_message)) {
|
||||
return array();
|
||||
}
|
||||
return $this->_message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience getter for testing.
|
||||
*
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
function _getMessage() {
|
||||
return $this->_message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for testing.
|
||||
*
|
||||
|
@ -108,14 +39,49 @@ class EmailTestComponent extends EmailComponent {
|
|||
return parent::_strip($content, $message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for testing.
|
||||
* DebugCompTransport class
|
||||
*
|
||||
* @return void
|
||||
* @package cake.tests.cases.libs.controller.components
|
||||
*/
|
||||
function formatAddress($string, $smtp = false) {
|
||||
return parent::_formatAddress($string, $smtp);
|
||||
class DebugCompTransport extends AbstractTransport {
|
||||
|
||||
/**
|
||||
* Last email
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $lastEmail = null;
|
||||
|
||||
/**
|
||||
* Send mail
|
||||
*
|
||||
* @params object $email CakeEmail
|
||||
* @return boolean
|
||||
*/
|
||||
public function send(CakeEmail $email) {
|
||||
$headers = $email->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), true));
|
||||
$to = $headers['To'];
|
||||
$subject = $headers['Subject'];
|
||||
unset($headers['To'], $headers['Subject']);
|
||||
|
||||
$message = implode("\n", $email->message());
|
||||
|
||||
$last = '<pre>';
|
||||
$last .= sprintf("%s %s\n", 'To:', $to);
|
||||
$last .= sprintf("%s %s\n", 'From:', $headers['From']);
|
||||
$last .= sprintf("%s %s\n", 'Subject:', $subject);
|
||||
$last .= sprintf("%s\n\n%s", 'Header:', $this->_headersToString($headers, "\n"));
|
||||
$last .= sprintf("%s\n\n%s", 'Message:', $message);
|
||||
$last .= '</pre>';
|
||||
|
||||
self::$lastEmail = $last;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -149,13 +115,6 @@ class EmailTestController extends Controller {
|
|||
*/
|
||||
public $components = array('Session', 'EmailTest');
|
||||
|
||||
/**
|
||||
* pageTitle property
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
public $pageTitle = 'EmailTest';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -196,7 +155,6 @@ class EmailComponentTest extends CakeTestCase {
|
|||
$this->Controller->Components->init($this->Controller);
|
||||
|
||||
$this->Controller->EmailTest->initialize($this->Controller, array());
|
||||
ClassRegistry::addObject('view', new View($this->Controller));
|
||||
|
||||
App::build(array(
|
||||
'View' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'View'. DS)
|
||||
|
@ -212,7 +170,6 @@ class EmailComponentTest extends CakeTestCase {
|
|||
function tearDown() {
|
||||
Configure::write('App.encoding', $this->_appEncoding);
|
||||
App::build();
|
||||
//$this->Controller->Session->delete('Message');
|
||||
ClassRegistry::flush();
|
||||
}
|
||||
|
||||
|
@ -234,50 +191,48 @@ class EmailComponentTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testSendFormats() {
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Cake SMTP test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$this->Controller->EmailTest->messageId = false;
|
||||
|
||||
$date = date(DATE_RFC2822);
|
||||
$message = <<<MSGBLOC
|
||||
<pre>To: postmaster@localhost
|
||||
<pre>To: postmaster@example.com
|
||||
From: noreply@example.com
|
||||
Subject: Cake SMTP test
|
||||
Header:
|
||||
|
||||
From: noreply@example.com
|
||||
Reply-To: noreply@example.com
|
||||
Date: $date
|
||||
X-Mailer: CakePHP Email Component
|
||||
Date: $date
|
||||
Content-Type: {CONTENTTYPE}
|
||||
Content-Transfer-Encoding: 7bitParameters:
|
||||
|
||||
Message:
|
||||
Content-Transfer-Encoding: 7bitMessage:
|
||||
|
||||
This is the body of the message
|
||||
|
||||
</pre>
|
||||
MSGBLOC;
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'text';
|
||||
$expect = str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $message);
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
|
||||
$this->assertEqual(DebugCompTransport::$lastEmail, $this->__osFix($expect));
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'html';
|
||||
$expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message);
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
|
||||
$this->assertEqual(DebugCompTransport::$lastEmail, $this->__osFix($expect));
|
||||
|
||||
// TODO: better test for format of message sent?
|
||||
$this->Controller->EmailTest->sendAs = 'both';
|
||||
$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"', $message);
|
||||
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
|
||||
$this->assertEqual(DebugCompTransport::$lastEmail, $this->__osFix($expect));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -289,35 +244,34 @@ MSGBLOC;
|
|||
function testTemplates() {
|
||||
ClassRegistry::flush();
|
||||
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Cake SMTP test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$this->Controller->EmailTest->messageId = false;
|
||||
|
||||
$date = date(DATE_RFC2822);
|
||||
$header = <<<HEADBLOC
|
||||
To: postmaster@localhost
|
||||
To: postmaster@example.com
|
||||
From: noreply@example.com
|
||||
Subject: Cake SMTP test
|
||||
Header:
|
||||
|
||||
From: noreply@example.com
|
||||
Reply-To: noreply@example.com
|
||||
Date: $date
|
||||
X-Mailer: CakePHP Email Component
|
||||
Date: $date
|
||||
Content-Type: {CONTENTTYPE}
|
||||
Content-Transfer-Encoding: 7bitParameters:
|
||||
|
||||
Message:
|
||||
Content-Transfer-Encoding: 7bitMessage:
|
||||
|
||||
|
||||
HEADBLOC;
|
||||
|
||||
$this->Controller->EmailTest->layout = 'default';
|
||||
$this->Controller->EmailTest->template = 'default';
|
||||
$this->Controller->set('title_for_layout', 'Email Test');
|
||||
|
||||
$text = <<<TEXTBLOC
|
||||
|
||||
|
@ -344,12 +298,12 @@ HTMLBLOC;
|
|||
$this->Controller->EmailTest->sendAs = 'text';
|
||||
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
|
||||
$this->assertEqual(DebugCompTransport::$lastEmail, $this->__osFix($expect));
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'html';
|
||||
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
|
||||
$this->assertEqual(DebugCompTransport::$lastEmail, $this->__osFix($expect));
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'both';
|
||||
$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"', $header);
|
||||
|
@ -358,7 +312,7 @@ HTMLBLOC;
|
|||
$expect = '<pre>' . $expect . '--alt---' . "\n\n" . '</pre>';
|
||||
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
|
||||
$this->assertEqual(DebugCompTransport::$lastEmail, $this->__osFix($expect));
|
||||
|
||||
$html = <<<HTMLBLOC
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
|
||||
|
@ -379,10 +333,7 @@ HTMLBLOC;
|
|||
$this->Controller->EmailTest->sendAs = 'html';
|
||||
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . '</pre>';
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message', 'default', 'thin'));
|
||||
//$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
|
||||
|
||||
$result = ClassRegistry::getObject('view');
|
||||
$this->assertFalse($result);
|
||||
$this->assertEqual(DebugCompTransport::$lastEmail, $this->__osFix($expect));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -391,12 +342,12 @@ HTMLBLOC;
|
|||
* @return void
|
||||
*/
|
||||
function testTemplateNestedElements() {
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Cake SMTP test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$this->Controller->EmailTest->messageId = false;
|
||||
$this->Controller->EmailTest->layout = 'default';
|
||||
$this->Controller->EmailTest->template = 'nested_element';
|
||||
|
@ -404,9 +355,9 @@ HTMLBLOC;
|
|||
$this->Controller->helpers = array('Html');
|
||||
|
||||
$this->Controller->EmailTest->send();
|
||||
//$result = $this->Controller->Session->read('Message.email.message');
|
||||
//$this->assertPattern('/Test/', $result);
|
||||
//$this->assertPattern('/http\:\/\/example\.com/', $result);
|
||||
$result = DebugCompTransport::$lastEmail;
|
||||
$this->assertPattern('/Test/', $result);
|
||||
$this->assertPattern('/http\:\/\/example\.com/', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -416,7 +367,7 @@ HTMLBLOC;
|
|||
* @return void
|
||||
*/
|
||||
function testSendDebug() {
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->cc = 'cc@example.com';
|
||||
$this->Controller->EmailTest->bcc = 'bcc@example.com';
|
||||
|
@ -424,12 +375,11 @@ HTMLBLOC;
|
|||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
/*
|
||||
$result = $this->Controller->Session->read('Message.email.message');
|
||||
$result = DebugCompTransport::$lastEmail;
|
||||
|
||||
$this->assertPattern('/To: postmaster@localhost\n/', $result);
|
||||
$this->assertPattern('/To: postmaster@example.com\n/', $result);
|
||||
$this->assertPattern('/Subject: Cake Debug Test\n/', $result);
|
||||
$this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
|
||||
$this->assertPattern('/From: noreply@example.com\n/', $result);
|
||||
|
@ -438,9 +388,8 @@ HTMLBLOC;
|
|||
$this->assertPattern('/Date: ' . preg_quote(date(DATE_RFC2822)) . '\n/', $result);
|
||||
$this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
|
||||
$this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
|
||||
$this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
|
||||
$this->assertPattern('/Content-Transfer-Encoding: 7bitMessage:\n/', $result);
|
||||
$this->assertPattern('/This is the body of the message/', $result);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -451,26 +400,26 @@ HTMLBLOC;
|
|||
function testSendDebugWithNoSessions() {
|
||||
$session = $this->Controller->Session;
|
||||
unset($this->Controller->Session);
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Cake Debug Test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
/*
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$result = $this->Controller->EmailTest->send('This is the body of the message');
|
||||
|
||||
$this->assertPattern('/To: postmaster@localhost\n/', $result);
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$this->Controller->EmailTest->send('This is the body of the message');
|
||||
$result = DebugCompTransport::$lastEmail;
|
||||
|
||||
$this->assertPattern('/To: postmaster@example.com\n/', $result);
|
||||
$this->assertPattern('/Subject: Cake Debug Test\n/', $result);
|
||||
$this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
|
||||
$this->assertPattern('/From: noreply@example.com\n/', $result);
|
||||
$this->assertPattern('/Date: ' . preg_quote(date(DATE_RFC2822)) . '\n/', $result);
|
||||
$this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
|
||||
$this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
|
||||
$this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
|
||||
$this->assertPattern('/Content-Transfer-Encoding: 7bitMessage:\n/', $result);
|
||||
$this->assertPattern('/This is the body of the message/', $result);
|
||||
$this->Controller->Session = $session;
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -484,14 +433,14 @@ HTMLBLOC;
|
|||
'View' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'View'. DS)
|
||||
));
|
||||
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Cake Debug Test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->layout = 'default';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
|
||||
$text = $html = 'This is the body of the message';
|
||||
|
||||
|
@ -525,14 +474,14 @@ HTMLBLOC;
|
|||
$this->Controller->set('value', 22091985);
|
||||
$this->Controller->set('title_for_layout', 'EmailTest');
|
||||
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Cake Debug Test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->layout = 'default';
|
||||
$this->Controller->EmailTest->template = 'custom';
|
||||
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
|
||||
$text = <<<TEXTBLOC
|
||||
|
||||
|
@ -578,29 +527,27 @@ HTMLBLOC;
|
|||
* @return void
|
||||
*/
|
||||
function testSendContentArray() {
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Cake Debug Test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
|
||||
$content = array('First line', 'Second line', 'Third line');
|
||||
$this->assertTrue($this->Controller->EmailTest->send($content));
|
||||
/*
|
||||
$result = $this->Controller->Session->read('Message.email.message');
|
||||
$result = DebugCompTransport::$lastEmail;
|
||||
|
||||
$this->assertPattern('/To: postmaster@localhost\n/', $result);
|
||||
$this->assertPattern('/To: postmaster@example.com\n/', $result);
|
||||
$this->assertPattern('/Subject: Cake Debug Test\n/', $result);
|
||||
$this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
|
||||
$this->assertPattern('/From: noreply@example.com\n/', $result);
|
||||
$this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
|
||||
$this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
|
||||
$this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
|
||||
$this->assertPattern('/Content-Transfer-Encoding: 7bitMessage:\n/', $result);
|
||||
$this->assertPattern('/First line\n/', $result);
|
||||
$this->assertPattern('/Second line\n/', $result);
|
||||
$this->assertPattern('/Third line\n/', $result);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -609,16 +556,16 @@ HTMLBLOC;
|
|||
* @return void
|
||||
*/
|
||||
function testDateProperty() {
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Cake Debug Test';
|
||||
$this->Controller->EmailTest->date = 'Today!';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
|
||||
$this->assertTrue($this->Controller->EmailTest->send('test message'));
|
||||
//$result = $this->Controller->Session->read('Message.email.message');
|
||||
//$this->assertPattern('/Date: Today!\n/', $result);
|
||||
$result = DebugCompTransport::$lastEmail;
|
||||
$this->assertPattern('/Date: Today!\n/', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -661,20 +608,20 @@ HTMLBLOC;
|
|||
mb_internal_encoding('ISO-8859-1');
|
||||
|
||||
$this->Controller->charset = 'UTF-8';
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'text';
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
|
||||
$subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
|
||||
|
||||
//preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
|
||||
//$this->assertEqual(trim($matches[1]), $subject);
|
||||
preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
|
||||
$this->assertEqual(trim($matches[1]), $subject);
|
||||
|
||||
$result = mb_internal_encoding();
|
||||
$this->assertEqual($result, 'ISO-8859-1');
|
||||
|
@ -690,29 +637,29 @@ HTMLBLOC;
|
|||
*/
|
||||
function testMultibyte() {
|
||||
$this->Controller->charset = 'UTF-8';
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
|
||||
$subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'text';
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
|
||||
//$this->assertEqual(trim($matches[1]), $subject);
|
||||
preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
|
||||
$this->assertEqual(trim($matches[1]), $subject);
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'html';
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
|
||||
//$this->assertEqual(trim($matches[1]), $subject);
|
||||
preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
|
||||
$this->assertEqual(trim($matches[1]), $subject);
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'both';
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
|
||||
//$this->assertEqual(trim($matches[1]), $subject);
|
||||
preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
|
||||
$this->assertEqual(trim($matches[1]), $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -721,12 +668,12 @@ HTMLBLOC;
|
|||
* @return void
|
||||
*/
|
||||
public function testSendWithAttachments() {
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Attachment Test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$this->Controller->EmailTest->attachments = array(
|
||||
__FILE__,
|
||||
'some-name.php' => __FILE__
|
||||
|
@ -735,9 +682,9 @@ HTMLBLOC;
|
|||
|
||||
$this->Controller->EmailTest->sendAs = 'text';
|
||||
$this->assertTrue($this->Controller->EmailTest->send($body));
|
||||
//$msg = $this->Controller->Session->read('Message.email.message');
|
||||
//$this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="EmailComponentTest.php"') . '/', $msg);
|
||||
//$this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="some-name.php"') . '/', $msg);
|
||||
$msg = DebugCompTransport::$lastEmail;
|
||||
$this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="EmailComponentTest.php"') . '/', $msg);
|
||||
$this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="some-name.php"') . '/', $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -746,36 +693,34 @@ HTMLBLOC;
|
|||
* @return void
|
||||
*/
|
||||
public function testSendAsIsNotIgnoredIfAttachmentsPresent() {
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Attachment Test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$this->Controller->EmailTest->attachments = array(__FILE__);
|
||||
$body = '<p>This is the body of the message</p>';
|
||||
|
||||
/*
|
||||
$this->Controller->EmailTest->sendAs = 'html';
|
||||
$this->assertTrue($this->Controller->EmailTest->send($body));
|
||||
$msg = $this->Controller->Session->read('Message.email.message');
|
||||
$msg = DebugCompTransport::$lastEmail;
|
||||
$this->assertNoPattern('/text\/plain/', $msg);
|
||||
$this->assertPattern('/text\/html/', $msg);
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'text';
|
||||
$this->assertTrue($this->Controller->EmailTest->send($body));
|
||||
$msg = $this->Controller->Session->read('Message.email.message');
|
||||
$msg = DebugCompTransport::$lastEmail;
|
||||
$this->assertPattern('/text\/plain/', $msg);
|
||||
$this->assertNoPattern('/text\/html/', $msg);
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'both';
|
||||
$this->assertTrue($this->Controller->EmailTest->send($body));
|
||||
$msg = $this->Controller->Session->read('Message.email.message');
|
||||
$msg = DebugCompTransport::$lastEmail;
|
||||
|
||||
$this->assertNoPattern('/text\/plain/', $msg);
|
||||
$this->assertNoPattern('/text\/html/', $msg);
|
||||
$this->assertPattern('/multipart\/alternative/', $msg);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -784,20 +729,20 @@ HTMLBLOC;
|
|||
* @return void
|
||||
*/
|
||||
public function testNoDoubleNewlinesInHeaders() {
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Attachment Test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$body = '<p>This is the body of the message</p>';
|
||||
|
||||
$this->Controller->EmailTest->sendAs = 'both';
|
||||
$this->assertTrue($this->Controller->EmailTest->send($body));
|
||||
//$msg = $this->Controller->Session->read('Message.email.message');
|
||||
$msg = DebugCompTransport::$lastEmail;
|
||||
|
||||
//$this->assertNoPattern('/\n\nContent-Transfer-Encoding/', $msg);
|
||||
//$this->assertPattern('/\nContent-Transfer-Encoding/', $msg);
|
||||
$this->assertNoPattern('/\n\nContent-Transfer-Encoding/', $msg);
|
||||
$this->assertPattern('/\nContent-Transfer-Encoding/', $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -817,18 +762,21 @@ HTMLBLOC;
|
|||
$this->Controller->EmailTest->date = 'Today!';
|
||||
$this->Controller->EmailTest->subject = 'Test subject';
|
||||
$this->Controller->EmailTest->additionalParams = 'X-additional-header';
|
||||
$this->Controller->EmailTest->delivery = 'smtp';
|
||||
$this->Controller->EmailTest->smtpOptions['host'] = 'blah';
|
||||
$this->Controller->EmailTest->smtpOptions['timeout'] = 0.2;
|
||||
$this->Controller->EmailTest->attachments = array('attachment1', 'attachment2');
|
||||
$this->Controller->EmailTest->textMessage = 'This is the body of the message';
|
||||
$this->Controller->EmailTest->htmlMessage = 'This is the body of the message';
|
||||
$this->Controller->EmailTest->messageId = false;
|
||||
/*
|
||||
|
||||
try {
|
||||
$this->Controller->EmailTest->send('Should not work');
|
||||
$this->fail('No exception');
|
||||
} catch (SocketException $e) {
|
||||
$this->assertTrue(true, 'SocketException raised');
|
||||
}
|
||||
*/
|
||||
|
||||
$this->Controller->EmailTest->reset();
|
||||
|
||||
$this->assertNull($this->Controller->EmailTest->template);
|
||||
|
@ -841,9 +789,7 @@ HTMLBLOC;
|
|||
$this->assertNull($this->Controller->EmailTest->date);
|
||||
$this->assertNull($this->Controller->EmailTest->subject);
|
||||
$this->assertNull($this->Controller->EmailTest->additionalParams);
|
||||
$this->assertIdentical($this->Controller->EmailTest->getHeaders(), array());
|
||||
$this->assertNull($this->Controller->EmailTest->getBoundary());
|
||||
$this->assertIdentical($this->Controller->EmailTest->getMessage(), array());
|
||||
$this->assertNull($this->Controller->EmailTest->smtpError);
|
||||
$this->assertIdentical($this->Controller->EmailTest->attachments, array());
|
||||
$this->assertNull($this->Controller->EmailTest->textMessage);
|
||||
$this->assertTrue($this->Controller->EmailTest->messageId);
|
||||
|
@ -857,15 +803,17 @@ HTMLBLOC;
|
|||
|
||||
$this->Controller->view = 'TestPlugin.Email';
|
||||
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'CustomViewClass test';
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$body = 'Body of message';
|
||||
|
||||
$this->assertTrue($this->Controller->EmailTest->send($body));
|
||||
//$result = $this->Controller->Session->read('Message.email.message');
|
||||
//$this->assertPattern('/Body of message/', $result);
|
||||
$result = DebugCompTransport::$lastEmail;
|
||||
|
||||
$this->assertPattern('/Body of message/', $result);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -885,109 +833,31 @@ HTMLBLOC;
|
|||
* @return void
|
||||
*/
|
||||
function testMessageId() {
|
||||
$this->Controller->EmailTest->to = 'postmaster@localhost';
|
||||
$this->Controller->EmailTest->to = 'postmaster@example.com';
|
||||
$this->Controller->EmailTest->from = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->subject = 'Cake Debug Test';
|
||||
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
|
||||
$this->Controller->EmailTest->template = null;
|
||||
|
||||
$this->Controller->EmailTest->delivery = 'debug';
|
||||
$this->Controller->EmailTest->delivery = 'DebugComp';
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//$result = $this->Controller->Session->read('Message.email.message');
|
||||
//$this->assertPattern('/Message-ID: \<[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}@' . env('HTTP_HOST') . '\>\n/', $result);
|
||||
$result = DebugCompTransport::$lastEmail;
|
||||
|
||||
$this->Controller->EmailTest->messageId = '<22091985.998877@localhost>';
|
||||
$this->assertPattern('/Message-ID: \<[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}@' . env('HTTP_HOST') . '\>\n/', $result);
|
||||
|
||||
$this->Controller->EmailTest->messageId = '<22091985.998877@example.com>';
|
||||
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//$result = $this->Controller->Session->read('Message.email.message');
|
||||
//$this->assertPattern('/Message-ID: <22091985.998877@localhost>\n/', $result);
|
||||
$result = DebugCompTransport::$lastEmail;
|
||||
|
||||
$this->assertPattern('/Message-ID: <22091985.998877@example.com>\n/', $result);
|
||||
|
||||
$this->Controller->EmailTest->messageId = false;
|
||||
|
||||
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
|
||||
//$result = $this->Controller->Session->read('Message.email.message');
|
||||
//$this->assertNoPattern('/Message-ID:/', $result);
|
||||
$result = DebugCompTransport::$lastEmail;
|
||||
|
||||
$this->assertNoPattern('/Message-ID:/', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* testSendMessage method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testSendMessage() {
|
||||
/*
|
||||
$this->Controller->EmailTest->delivery = 'getMessage';
|
||||
$this->Controller->EmailTest->lineLength = 70;
|
||||
|
||||
$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
|
||||
$this->Controller->EmailTest->sendAs = 'text';
|
||||
$result = $this->Controller->EmailTest->send($text);
|
||||
$expected = array(
|
||||
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do',
|
||||
'eiusmod tempor incididunt ut labore et dolore magna aliqua.',
|
||||
'',
|
||||
''
|
||||
);
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$text = 'Lorem ipsum dolor sit amet, <b>consectetur</b> adipisicing elit, sed do <span>eiusmod tempor</span> incididunt ut labore et dolore magna aliqua.';
|
||||
$this->Controller->EmailTest->sendAs = 'html';
|
||||
$result = $this->Controller->EmailTest->send($text);
|
||||
$expected = array(
|
||||
$text,
|
||||
'',
|
||||
''
|
||||
);
|
||||
$this->assertEqual($expected, $result);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that _formatName doesn't jack up email addresses with alias parts.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testFormatAddressAliases() {
|
||||
$result = $this->Controller->EmailTest->formatAddress('email@example.com');
|
||||
$this->assertEqual($result, 'email@example.com');
|
||||
|
||||
$result = $this->Controller->EmailTest->formatAddress('alias <email@example.com>');
|
||||
$this->assertEqual($result, 'alias <email@example.com>');
|
||||
|
||||
$result = $this->Controller->EmailTest->formatAddress('alias<email@example.com>');
|
||||
$this->assertEqual($result, 'alias <email@example.com>');
|
||||
|
||||
$result = $this->Controller->EmailTest->formatAddress('email@example.com');
|
||||
$this->assertEqual($result, 'email@example.com');
|
||||
|
||||
$result = $this->Controller->EmailTest->formatAddress('<email@example.com>');
|
||||
$this->assertEqual($result, '<email@example.com>');
|
||||
|
||||
$result = $this->Controller->EmailTest->formatAddress('email@example.com', true);
|
||||
$this->assertEqual($result, '<email@example.com>');
|
||||
|
||||
$result = $this->Controller->EmailTest->formatAddress('<email@example.com>', true);
|
||||
$this->assertEqual($result, '<email@example.com>');
|
||||
|
||||
$result = $this->Controller->EmailTest->formatAddress('alias name <email@example.com>', true);
|
||||
$this->assertEqual($result, '<email@example.com>');
|
||||
}
|
||||
|
||||
/**
|
||||
* test formatting addresses with multibyte chars
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testFormatAddressMultibyte() {
|
||||
$this->Controller->EmailTest->charset = 'UTF-8';
|
||||
$result = $this->Controller->EmailTest->formatAddress('ÄÖÜTest <email@domain.de>');
|
||||
$this->assertEqual($result, '=?UTF-8?B?w4TDlsOcVGVzdCA=?= <email@domain.de>');
|
||||
|
||||
$result = $this->Controller->EmailTest->formatAddress('ÄÖÜTest<email@domain.de>');
|
||||
$this->assertEqual($result, '=?UTF-8?B?w4TDlsOcVGVzdA==?= <email@domain.de>');
|
||||
|
||||
$result = $this->Controller->EmailTest->formatAddress('ÄÖÜTest <email@domain.de>', true);
|
||||
$this->assertEqual($result, '<email@domain.de>');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue