Merge with 2.0-email.

This commit is contained in:
Juan Basso 2011-04-23 22:36:55 -04:00
commit 24fbaafe01
32 changed files with 3137 additions and 1146 deletions

View file

@ -0,0 +1,88 @@
<?php
/**
* This is email configuration file.
*
* Use it to configure email transports of Cake.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.config
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* In this file you set up your send email details.
*
* @package cake.config
*/
/**
* Email configuration class.
* You can specify multiple configurations for production, development and testing.
*
* transport => The name of a supported transport; valid options are as follows:
* mail - Send using PHP mail function
* smtp - Send using SMTP
*
* You can add custom transports (or override existing transports) by adding the
* appropriate file to app/Network/Email. Transports should be named 'YourTransport.php',
* where 'Your' is the name of the transport.
*
* from =>
* The origin email. See CakeEmail::from() about the valid values
*
*/
class EmailConfig {
public $default = array(
'transport' => 'mail',
'from' => 'you@localhost'
);
public $smtp = array(
'transport' => 'smtp',
'from' => array('My Site', 'site@localhost'),
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null
);
public $fast = array(
'from' => 'you@localhost',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
'replyTo' => null,
'readReceipt' => null,
'returnPath' => null,
'messageId' => true,
'subject' => null,
'message' => null,
'headers' => null,
'viewRender' => null,
'template' => false,
'layout' => false,
'viewVars' => null,
'attachments' => null,
'emailFormat' => null,
'transport' => 'smtp',
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null
);
}

View file

@ -0,0 +1,88 @@
<?php
/**
* This is email configuration file.
*
* Use it to configure email transports of Cake.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.config
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* In this file you set up your send email details.
*
* @package cake.config
*/
/**
* Email configuration class.
* You can specify multiple configurations for production, development and testing.
*
* transport => The name of a supported transport; valid options are as follows:
* mail - Send using PHP mail function
* smtp - Send using SMTP
*
* You can add custom transports (or override existing transports) by adding the
* appropriate file to app/Network/Email. Transports should be named 'YourTransport.php',
* where 'Your' is the name of the transport.
*
* from =>
* The origin email. See CakeEmail::from() about the valid values
*
*/
class EmailConfig {
public $default = array(
'transport' => 'mail',
'from' => 'you@localhost'
);
public $smtp = array(
'transport' => 'smtp',
'from' => array('My Site', 'site@localhost'),
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null
);
public $fast = array(
'from' => 'you@localhost',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
'replyTo' => null,
'readReceipt' => null,
'returnPath' => null,
'messageId' => true,
'subject' => null,
'message' => null,
'headers' => null,
'viewRender' => null,
'template' => false,
'layout' => false,
'viewVars' => null,
'attachments' => null,
'emailFormat' => null,
'transport' => 'smtp',
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null
);
}

View file

@ -19,6 +19,7 @@
App::uses('Component', 'Controller'); App::uses('Component', 'Controller');
App::uses('Multibyte', 'I18n'); App::uses('Multibyte', 'I18n');
App::uses('CakeEmail', 'Network');
/** /**
* EmailComponent * EmailComponent
@ -28,7 +29,7 @@ App::uses('Multibyte', 'I18n');
* *
* @package cake.libs.controller.components * @package cake.libs.controller.components
* @link http://book.cakephp.org/view/1283/Email * @link http://book.cakephp.org/view/1283/Email
* * @deprecated Use Network/CakeEmail
*/ */
class EmailComponent extends Component { class EmailComponent extends Component {
@ -149,14 +150,6 @@ class EmailComponent extends Component {
*/ */
public $template = null; 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 * Line feed character(s) to be used when sending using mail() function
* By default PHP_EOL is used. * By default PHP_EOL is used.
@ -167,12 +160,7 @@ class EmailComponent extends Component {
* @var string * @var string
* @access public * @access public
*/ */
var $lineFeed = PHP_EOL; public $lineFeed = PHP_EOL;
/**
* @deprecated see lineLength
*/
protected $_lineLength = null;
/** /**
* What format should the email be sent in * What format should the email be sent in
@ -251,15 +239,6 @@ class EmailComponent extends Component {
*/ */
public $smtpOptions = array(); public $smtpOptions = array();
/**
* Placeholder for any errors that might happen with the
* smtp mail methods
*
* @var string
* @access public
*/
public $smtpError = null;
/** /**
* Contains the rendered plain text message if one was sent. * Contains the rendered plain text message if one was sent.
* *
@ -291,36 +270,11 @@ class EmailComponent extends Component {
public $messageId = true; public $messageId = true;
/** /**
* Temporary store of message header lines * Controller reference
* *
* @var array * @var object Controller
* @access protected
*/ */
protected $_header = array(); protected $_controller = null;
/**
* 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();
/**
* Variable that holds SMTP connection
*
* @var resource
* @access protected
*/
protected $_smtpConnection = null;
/** /**
* Constructor * Constructor
@ -329,7 +283,7 @@ class EmailComponent extends Component {
* @param array $settings Array of configuration settings. * @param array $settings Array of configuration settings.
*/ */
public function __construct(ComponentCollection $collection, $settings = array()) { public function __construct(ComponentCollection $collection, $settings = array()) {
$this->Controller = $collection->getController(); $this->_controller = $collection->getController();
parent::__construct($collection, $settings); parent::__construct($collection, $settings);
} }
@ -344,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 * Send an email using the specified content, template and layout
* *
@ -361,59 +308,71 @@ class EmailComponent extends Component {
* @return boolean Success * @return boolean Success
*/ */
public function send($content = null, $template = null, $layout = null) { 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('X-Mailer' => $this->xMailer);
foreach ($this->headers as $key => $value) {
$headers['X-' . $key] = $value;
}
if ($this->date != false) {
$headers['Date'] = $this->date;
}
$lib->setHeaders($headers);
if ($template) { if ($template) {
$this->template = $template; $this->template = $template;
} }
if ($layout) { if ($layout) {
$this->layout = $layout; $this->layout = $layout;
} }
$lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs);
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;
if (!empty($this->attachments)) { if (!empty($this->attachments)) {
$this->_attachFiles(); $lib->attachments($this->_formatAttachFiles());
} }
if (!is_null($this->_boundary)) { $lib->transport($this->delivery);
$this->_message[] = ''; if ($this->delivery === 'mail') {
$this->_message[] = '--' . $this->_boundary . '--'; $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams));
$this->_message[] = ''; } elseif ($this->delivery === 'smtp') {
$lib->config($this->smtpOptions);
} else {
$lib->config(array());
} }
$sent = $lib->send($content);
$_method = '_' . $this->delivery; $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML);
$sent = $this->$_method(); 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->_header = array();
$this->_message = array(); $this->_message = array();
@ -437,232 +396,18 @@ class EmailComponent extends Component {
$this->subject = null; $this->subject = null;
$this->additionalParams = null; $this->additionalParams = null;
$this->date = null; $this->date = null;
$this->smtpError = null;
$this->attachments = array(); $this->attachments = array();
$this->htmlMessage = null; $this->htmlMessage = null;
$this->textMessage = null; $this->textMessage = null;
$this->messageId = true; $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
* @return array Email ready to be sent
* @access private
*/ */
function _render($content) { protected function _formatAttachFiles() {
$viewClass = $this->Controller->viewClass;
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() {
$files = array(); $files = array();
foreach ($this->attachments as $filename => $attachment) { foreach ($this->attachments as $filename => $attachment) {
$file = $this->_findFiles($attachment); $file = $this->_findFiles($attachment);
@ -673,21 +418,7 @@ class EmailComponent extends Component {
$files[$filename] = $file; $files[$filename] = $file;
} }
} }
return $files;
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[] = '';
}
} }
/** /**
@ -710,39 +441,6 @@ class EmailComponent extends Component {
return null; 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 * Encode the specified string using the current charset
* *
@ -770,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 * @param array $addresses
* @return string Email address suitable for email headers or smtp pipe * @return array
* @access private
*/ */
function _formatAddress($string, $smtp = false) { protected function _formatAddresses($addresses) {
$hasAlias = preg_match('/((.*))?\s?<(.+)>/', $string, $matches); $formatted = array();
if ($smtp && $hasAlias) { foreach ($addresses as $address) {
return $this->_strip('<' . $matches[3] . '>'); if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) {
} elseif ($smtp) { $formatted[$this->_strip($matches[3])] = $this->_encode($matches[2]);
return $this->_strip('<' . $string . '>'); } else {
$address = $this->_strip($address);
$formatted[$address] = $address;
}
} }
return $formatted;
if ($hasAlias && !empty($matches[2])) {
return $this->_encode($matches[2]) . $this->_strip(' <' . $matches[3] . '>');
}
return $this->_strip($string);
} }
/** /**
@ -813,197 +509,4 @@ class EmailComponent extends Component {
return $value; return $value;
} }
/**
* Wrapper for PHP mail function used for sending out emails
*
* @return bool Success
* @access private
*/
function _mail() {
$header = implode($this->lineFeed, $this->_header);
$message = implode($this->lineFeed, $this->_message);
if (is_array($this->to)) {
$to = implode(', ', array_map(array($this, '_formatAddress'), $this->to));
} else {
$to = $this->to;
}
if (ini_get('safe_mode')) {
return @mail($to, $this->_encode($this->subject), $message, $header);
}
return @mail($to, $this->_encode($this->subject), $message, $header, $this->additionalParams);
}
/**
* Helper method to get socket, overridden in tests
*
* @param array $config Config data for the socket.
* @return void
* @access protected
*/
function _getSocket($config) {
$this->_smtpConnection = new CakeSocket($config);
}
/**
* Sends out email via SMTP
*
* @return bool Success
* @access private
*/
function _smtp() {
App::uses('CakeSocket', 'Network');
$defaults = array(
'host' => 'localhost',
'port' => 25,
'protocol' => 'smtp',
'timeout' => 30
);
$this->smtpOptions = array_merge($defaults, $this->smtpOptions);
$this->_getSocket($this->smtpOptions);
if (!$this->_smtpConnection->connect()) {
$this->smtpError = $this->_smtpConnection->lastError();
return false;
} elseif (!$this->_smtpSend(null, '220')) {
return false;
}
$httpHost = env('HTTP_HOST');
if (isset($this->smtpOptions['client'])) {
$host = $this->smtpOptions['client'];
} elseif (!empty($httpHost)) {
list($host) = explode(':', $httpHost);
} else {
$host = 'localhost';
}
if (!$this->_smtpSend("EHLO {$host}", '250') && !$this->_smtpSend("HELO {$host}", '250')) {
return false;
}
if (isset($this->smtpOptions['username']) && isset($this->smtpOptions['password'])) {
$authRequired = $this->_smtpSend('AUTH LOGIN', '334|503');
if ($authRequired == '334') {
if (!$this->_smtpSend(base64_encode($this->smtpOptions['username']), '334')) {
return false;
}
if (!$this->_smtpSend(base64_encode($this->smtpOptions['password']), '235')) {
return false;
}
} elseif ($authRequired != '503') {
return false;
}
}
if (!$this->_smtpSend('MAIL FROM: ' . $this->_formatAddress($this->from, true))) {
return false;
}
if (!is_array($this->to)) {
$tos = array_map('trim', explode(',', $this->to));
} else {
$tos = $this->to;
}
foreach ($tos as $to) {
if (!$this->_smtpSend('RCPT TO: ' . $this->_formatAddress($to, true))) {
return false;
}
}
foreach ($this->cc as $cc) {
if (!$this->_smtpSend('RCPT TO: ' . $this->_formatAddress($cc, true))) {
return false;
}
}
foreach ($this->bcc as $bcc) {
if (!$this->_smtpSend('RCPT TO: ' . $this->_formatAddress($bcc, true))) {
return false;
}
}
if (!$this->_smtpSend('DATA', '354')) {
return false;
}
$header = implode("\r\n", $this->_header);
$message = implode("\r\n", $this->_message);
if (!$this->_smtpSend($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.")) {
return false;
}
$this->_smtpSend('QUIT', false);
$this->_smtpConnection->disconnect();
return true;
}
/**
* Protected method for sending data to SMTP connection
*
* @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
* @access protected
*/
function _smtpSend($data, $checkCode = '250') {
if (!is_null($data)) {
$this->_smtpConnection->write($data . "\r\n");
}
while ($checkCode !== false) {
$response = '';
$startTime = time();
while (substr($response, -2) !== "\r\n" && ((time() - $startTime) < $this->smtpOptions['timeout'])) {
$response .= $this->_smtpConnection->read();
}
if (substr($response, -2) !== "\r\n") {
$this->smtpError = 'timeout';
return false;
}
$response = end(explode("\r\n", rtrim($response, "\r\n")));
if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) {
if ($code[2] === '-') {
continue;
}
return $code[1];
}
$this->smtpError = $response;
return false;
}
return true;
}
/**
* Set as controller flash message a debug message showing current settings in component
*
* @return boolean Success
* @access private
*/
function _debug() {
$nl = "\n";
$header = implode($nl, $this->_header);
$message = implode($nl, $this->_message);
$fm = '<pre>';
if (is_array($this->to)) {
$to = implode(', ', array_map(array($this, '_formatAddress'), $this->to));
} else {
$to = $this->to;
}
$fm .= sprintf('%s %s%s', 'To:', $to, $nl);
$fm .= sprintf('%s %s%s', 'From:', $this->from, $nl);
$fm .= sprintf('%s %s%s', 'Subject:', $this->_encode($this->subject), $nl);
$fm .= sprintf('%s%3$s%3$s%s', 'Header:', $header, $nl);
$fm .= sprintf('%s%3$s%3$s%s', 'Parameters:', $this->additionalParams, $nl);
$fm .= sprintf('%s%3$s%3$s%s', 'Message:', $message, $nl);
$fm .= '</pre>';
if (isset($this->Controller->Session)) {
$this->Controller->Session->setFlash($fm, 'default', null, 'email');
return true;
}
return $fm;
}
} }

View file

@ -459,8 +459,8 @@ class CakeSessionException extends CakeException { }
class ConfigureException extends CakeException { } class ConfigureException extends CakeException { }
/** /**
* Exception class for Socket. This exception will be thrown from CakeSocket, HttpSocket and HttpResponse when it * Exception class for Socket. This exception will be thrown from CakeSocket, CakeEmail, HttpSocket
* encounters an error. * and HttpResponse when it encounters an error.
* *
* @package cake.libs * @package cake.libs
*/ */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,75 @@
<?php
/**
* Abstract send email
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake.libs.email
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Abstract class
*
* @package cake.libs.email
*/
abstract class AbstractTransport {
/**
* Configurations
*
* @var array
*/
protected $_config = array();
/**
* Send mail
*
* @params object $email CakeEmail
* @return boolean
*/
abstract public function send(CakeEmail $email);
/**
* Set the config
*
* @param array $config
* @return object $this
*/
public function config($config = array()) {
if (!empty($config)) {
$this->_config = $config;
}
}
/**
* Help to convert headers in string
*
* @param array $headers Headers in format key => value
* @param string $eol
* @return string
*/
protected function _headersToString($headers, $eol = "\r\n") {
$out = '';
foreach ($headers as $key => $value) {
if ($value === false || $value === null || $value === '') {
continue;
}
$out .= $key . ': ' . $value . $eol;
}
if (!empty($out)) {
$out = substr($out, 0, -1 * strlen($eol));
}
return $out;
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
* Send mail using mail() function
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake.libs.email
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Mail class
*
* @package cake.libs.email
*/
class MailTransport extends AbstractTransport {
/**
* Send mail
*
* @params object $email CakeEmail
* @return boolean
*/
public function send(CakeEmail $email) {
$eol = PHP_EOL;
if (isset($this->_config['eol'])) {
$eol = $this->_config['eol'];
}
$headers = $email->getHeaders(array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true));
$to = $headers['To'];
unset($headers['To']);
$header = $this->_headersToString($headers, $eol);
$message = implode($eol, $email->message());
if (ini_get('safe_mode') || !isset($this->_config['additionalParameters'])) {
return @mail($to, $email->subject(), $message, $header);
}
return @mail($to, $email->subject(), $message, $header, $this->_config['additionalParameters']);
}
}

View file

@ -0,0 +1,221 @@
<?php
/**
* Send mail using SMTP protocol
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake.libs.email
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('CakeSocket', 'Network');
/**
* SendEmail class
*
* @package cake.libs.email
*/
class SmtpTransport extends AbstractTransport {
/**
* Socket to SMTP server
*
* @var object CakeScoket
*/
protected $_socket;
/**
* CakeEmail
*
* @var object CakeEmail
*/
protected $_cakeEmail;
/**
* Send mail
*
* @params object $email CakeEmail
* @return boolean
* @thrown SocketException
*/
public function send(CakeEmail $email) {
$this->_cakeEmail = $email;
$this->_connect();
$this->_auth();
$this->_sendRcpt();
$this->_sendData();
$this->_disconnect();
return true;
}
/**
* Set the configuration
*
* @param array $config
* @return object $this
*/
public function config($config = array()) {
$default = array(
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => null,
'password' => null,
'client' => null
);
$this->_config = $config + $default;
}
/**
* Connect to SMTP Server
*
* @return void
* @thrown SocketException
*/
protected function _connect() {
$this->_generateSocket();
if (!$this->_socket->connect()) {
throw new SocketException(__d('cake', 'Unable to connect in SMTP server.'));
}
$this->_smtpSend(null, '220');
if (isset($this->_config['client'])) {
$host = $this->_config['client'];
} elseif ($httpHost = env('HTTP_HOST')) {
list($host) = explode(':', $httpHost);
} else {
$host = 'localhost';
}
try {
$this->_smtpSend("EHLO {$host}", '250');
} catch (SocketException $e) {
try {
$this->_smtpSend("HELO {$host}", '250');
} catch (SocketException $e2) {
throw new SocketException(__d('cake', 'SMTP server not accepted the connection.'));
}
}
}
/**
* Send authentication
*
* @return void
* @thrown SocketException
*/
protected function _auth() {
if (isset($this->_config['username']) && isset($this->_config['password'])) {
$authRequired = $this->_smtpSend('AUTH LOGIN', '334|503');
if ($authRequired == '334') {
if (!$this->_smtpSend(base64_encode($this->_config['username']), '334')) {
throw new SocketException(__d('cake', 'SMTP server not accepted the username.'));
}
if (!$this->_smtpSend(base64_encode($this->_config['password']), '235')) {
throw new SocketException(__d('cake', 'SMTP server not accepted the password.'));
}
} elseif ($authRequired != '503') {
throw new SocketException(__d('cake', 'SMTP do not require authentication.'));
}
}
}
/**
* Send emails
*
* @return void
* @thrown SocketException
*/
protected function _sendRcpt() {
$from = $this->_cakeEmail->from();
$this->_smtpSend('MAIL FROM:<' . key($from) . '>');
$to = $this->_cakeEmail->to();
$cc = $this->_cakeEmail->cc();
$bcc = $this->_cakeEmail->bcc();
$emails = array_merge(array_keys($to), array_keys($cc), array_keys($bcc));
foreach ($emails as $email) {
$this->_smtpSend('RCPT TO:<' . $email . '>');
}
}
/**
* Send Data
*
* @return void
* @thrown SocketException
*/
protected function _sendData() {
$this->_smtpSend('DATA', '354');
$headers = $this->_cakeEmail->getHeaders(array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), true));
$headers = $this->_headersToString($headers);
$message = implode("\r\n", $this->_cakeEmail->message());
$this->_smtpSend($headers . "\r\n\r\n" . $message . "\r\n\r\n\r\n.");
}
/**
* Disconnect
*
* @return void
* @thrown SocketException
*/
protected function _disconnect() {
$this->_smtpSend('QUIT', false);
$this->_socket->disconnect();
}
/**
* Helper method to generate socket
*
* @return void
* @thrown SocketException
*/
protected function _generateSocket() {
$this->_socket = new CakeSocket($this->_config);
}
/**
* Protected method for sending data to SMTP connection
*
* @param string $data data to be sent to SMTP server
* @param mixed $checkCode code to check for in server response, false to skip
* @return void
* @thrown SocketException
*/
function _smtpSend($data, $checkCode = '250') {
if (!is_null($data)) {
$this->_socket->write($data . "\r\n");
}
while ($checkCode !== false) {
$response = '';
$startTime = time();
while (substr($response, -2) !== "\r\n" && ((time() - $startTime) < $this->_config['timeout'])) {
$response .= $this->_socket->read();
}
if (substr($response, -2) !== "\r\n") {
throw new SocketException(__d('cake', 'SMTP timeout.'));
}
$response = end(explode("\r\n", rtrim($response, "\r\n")));
if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) {
if ($code[2] === '-') {
continue;
}
return $code[1];
}
throw new SocketException(__d('cake', 'SMTP Error: %s', $response));
}
}
}

View file

@ -0,0 +1,827 @@
<?php
/**
* CakeEmailTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake.tests.cases.libs
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('CakeEmail', 'Network');
/**
* Help to test CakeEmail
*
*/
class TestCakeEmail extends CakeEmail {
/**
* Config
*
*/
protected $_config = array();
/**
* Wrap to protected method
*
*/
public function formatAddress($address) {
return parent::_formatAddress($address);
}
/**
* Wrap to protected method
*
*/
public function wrap($text) {
return parent::_wrap($text);
}
/**
* Get the boundary attribute
*
* @return string
*/
public function getBoundary() {
return $this->_boundary;
}
}
/**
* Debug transport email
*
*/
class DebugTransport extends AbstractTransport {
/**
* Last email body
*
* @var string
*/
public static $lastEmail = '';
/**
* Last email header
*
* @var string
*/
public static $lastHeader = '';
/**
* Include addresses in header
*
* @var boolean
*/
public static $includeAddresses = false;
/**
* Config
*
* @var array
*/
public static $config = array();
/**
* Config
*
* @param mixed $config
* @return mixed
*/
public function config($config) {
self::$config = $config;
}
/**
* Send
*
* @param object $email CakeEmail
* @return boolean
*/
public function send(CakeEmail $email) {
self::$lastEmail = implode("\r\n", $email->message());
$options = array();
if (self::$includeAddresses) {
$options = array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true);
}
self::$lastHeader = $this->_headersToString($email->getHeaders($options));
return true;
}
}
/**
* CakeEmailTest class
*
* @package cake.tests.cases.libs
*/
class CakeEmailTest extends CakeTestCase {
/**
* setUp
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->CakeEmail = new TestCakeEmail();
App::build(array(
'views' => array(CAKE . 'tests' . DS . 'test_app' . DS . 'View'. DS)
));
}
/**
* tearDown method
*
* @return void
*/
function tearDown() {
parent::tearDown();
App::build();
}
/**
* testFrom method
*
* @return void
*/
public function testFrom() {
$this->assertIdentical($this->CakeEmail->from(), array());
$this->CakeEmail->from('cake@cakephp.org');
$expected = array('cake@cakephp.org' => 'cake@cakephp.org');
$this->assertIdentical($this->CakeEmail->from(), $expected);
$this->CakeEmail->from(array('cake@cakephp.org'));
$this->assertIdentical($this->CakeEmail->from(), $expected);
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
$expected = array('cake@cakephp.org' => 'CakePHP');
$this->assertIdentical($this->CakeEmail->from(), $expected);
$result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP'));
$this->assertIdentical($this->CakeEmail->from(), $expected);
$this->assertIdentical($this->CakeEmail, $result);
}
/**
* testSender method
*
* @return void
*/
public function testSender() {
$this->CakeEmail->reset();
$this->assertIdentical($this->CakeEmail->sender(), array());
$this->CakeEmail->sender('cake@cakephp.org', 'Name');
$expected = array('cake@cakephp.org' => 'Name');
$this->assertIdentical($this->CakeEmail->sender(), $expected);
$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
$this->assertIdentical($headers['From'], false);
$this->assertIdentical($headers['Sender'], 'Name <cake@cakephp.org>');
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
$this->assertIdentical($headers['From'], 'CakePHP <cake@cakephp.org>');
$this->assertIdentical($headers['Sender'], '');
}
/**
* testTo method
*
* @return void
*/
public function testTo() {
$this->assertIdentical($this->CakeEmail->to(), array());
$result = $this->CakeEmail->to('cake@cakephp.org');
$expected = array('cake@cakephp.org' => 'cake@cakephp.org');
$this->assertIdentical($this->CakeEmail->to(), $expected);
$this->assertIdentical($this->CakeEmail, $result);
$this->CakeEmail->to('cake@cakephp.org', 'CakePHP');
$expected = array('cake@cakephp.org' => 'CakePHP');
$this->assertIdentical($this->CakeEmail->to(), $expected);
$list = array(
'cake@cakephp.org' => 'Cake PHP',
'cake-php@googlegroups.com' => 'Cake Groups',
'root@cakephp.org'
);
$this->CakeEmail->to($list);
$expected = array(
'cake@cakephp.org' => 'Cake PHP',
'cake-php@googlegroups.com' => 'Cake Groups',
'root@cakephp.org' => 'root@cakephp.org'
);
$this->assertIdentical($this->CakeEmail->to(), $expected);
$this->CakeEmail->addTo('jrbasso@cakephp.org');
$this->CakeEmail->addTo('mark_story@cakephp.org', 'Mark Story');
$result = $this->CakeEmail->addTo(array('phpnut@cakephp.org' => 'PhpNut', 'jose_zap@cakephp.org'));
$expected = array(
'cake@cakephp.org' => 'Cake PHP',
'cake-php@googlegroups.com' => 'Cake Groups',
'root@cakephp.org' => 'root@cakephp.org',
'jrbasso@cakephp.org' => 'jrbasso@cakephp.org',
'mark_story@cakephp.org' => 'Mark Story',
'phpnut@cakephp.org' => 'PhpNut',
'jose_zap@cakephp.org' => 'jose_zap@cakephp.org'
);
$this->assertIdentical($this->CakeEmail->to(), $expected);
$this->assertIdentical($this->CakeEmail, $result);
}
/**
* Data provider function for testBuildInvalidData
*
* @return array
*/
public static function invalidEmails() {
return array(
array(1.0),
array(''),
array('string'),
array('<tag>'),
array('some@one.whereis'),
array(array('ok@cakephp.org', 1.0, '', 'string'))
);
}
/**
* testBuildInvalidData
*
* @dataProvider invalidEmails
* @expectedException SocketException
* @return void
*/
public function testInvalidEmail($value) {
$this->CakeEmail->to($value);
}
/**
* testFormatAddress method
*
* @return void
*/
public function testFormatAddress() {
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org'));
$expected = array('cake@cakephp.org');
$this->assertIdentical($result, $expected);
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org', 'php@cakephp.org' => 'php@cakephp.org'));
$expected = array('cake@cakephp.org', 'php@cakephp.org');
$this->assertIdentical($result, $expected);
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'CakePHP', 'php@cakephp.org' => 'Cake'));
$expected = array('CakePHP <cake@cakephp.org>', 'Cake <php@cakephp.org>');
$this->assertIdentical($result, $expected);
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'ÄÖÜTest'));
$expected = array('=?UTF-8?B?w4TDlsOcVGVzdA==?= <cake@cakephp.org>');
$this->assertIdentical($result, $expected);
}
/**
* testAddresses method
*
* @return void
*/
public function testAddresses() {
$this->CakeEmail->reset();
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
$this->CakeEmail->replyTo('replyto@cakephp.org', 'ReplyTo CakePHP');
$this->CakeEmail->readReceipt('readreceipt@cakephp.org', 'ReadReceipt CakePHP');
$this->CakeEmail->returnPath('returnpath@cakephp.org', 'ReturnPath CakePHP');
$this->CakeEmail->to('to@cakephp.org', 'To CakePHP');
$this->CakeEmail->cc('cc@cakephp.org', 'Cc CakePHP');
$this->CakeEmail->bcc('bcc@cakephp.org', 'Bcc CakePHP');
$this->CakeEmail->addTo('to2@cakephp.org', 'To2 CakePHP');
$this->CakeEmail->addCc('cc2@cakephp.org', 'Cc2 CakePHP');
$this->CakeEmail->addBcc('bcc2@cakephp.org', 'Bcc2 CakePHP');
$this->assertIdentical($this->CakeEmail->from(), array('cake@cakephp.org' => 'CakePHP'));
$this->assertIdentical($this->CakeEmail->replyTo(), array('replyto@cakephp.org' => 'ReplyTo CakePHP'));
$this->assertIdentical($this->CakeEmail->readReceipt(), array('readreceipt@cakephp.org' => 'ReadReceipt CakePHP'));
$this->assertIdentical($this->CakeEmail->returnPath(), array('returnpath@cakephp.org' => 'ReturnPath CakePHP'));
$this->assertIdentical($this->CakeEmail->to(), array('to@cakephp.org' => 'To CakePHP', 'to2@cakephp.org' => 'To2 CakePHP'));
$this->assertIdentical($this->CakeEmail->cc(), array('cc@cakephp.org' => 'Cc CakePHP', 'cc2@cakephp.org' => 'Cc2 CakePHP'));
$this->assertIdentical($this->CakeEmail->bcc(), array('bcc@cakephp.org' => 'Bcc CakePHP', 'bcc2@cakephp.org' => 'Bcc2 CakePHP'));
$headers = $this->CakeEmail->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true));
$this->assertIdentical($headers['From'], 'CakePHP <cake@cakephp.org>');
$this->assertIdentical($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>');
$this->assertIdentical($headers['Disposition-Notification-To'], 'ReadReceipt CakePHP <readreceipt@cakephp.org>');
$this->assertIdentical($headers['Return-Path'], 'ReturnPath CakePHP <returnpath@cakephp.org>');
$this->assertIdentical($headers['To'], 'To CakePHP <to@cakephp.org>, To2 CakePHP <to2@cakephp.org>');
$this->assertIdentical($headers['Cc'], 'Cc CakePHP <cc@cakephp.org>, Cc2 CakePHP <cc2@cakephp.org>');
$this->assertIdentical($headers['Bcc'], 'Bcc CakePHP <bcc@cakephp.org>, Bcc2 CakePHP <bcc2@cakephp.org>');
}
/**
* testMessageId method
*
* @return void
*/
public function testMessageId() {
$this->CakeEmail->messageId(true);
$result = $this->CakeEmail->getHeaders();
$this->assertTrue(isset($result['Message-ID']));
$this->CakeEmail->messageId(false);
$result = $this->CakeEmail->getHeaders();
$this->assertFalse(isset($result['Message-ID']));
$result = $this->CakeEmail->messageId('<my-email@localhost>');
$this->assertIdentical($this->CakeEmail, $result);
$result = $this->CakeEmail->getHeaders();
$this->assertIdentical($result['Message-ID'], '<my-email@localhost>');
}
/**
* testMessageIdInvalid method
*
* @return void
* @expectedException SocketException
*/
public function testMessageIdInvalid() {
$this->CakeEmail->messageId('my-email@localhost');
}
/**
* testSubject method
*
* @return void
*/
public function testSubject() {
$this->CakeEmail->subject('You have a new message.');
$this->assertIdentical($this->CakeEmail->subject(), 'You have a new message.');
$this->CakeEmail->subject(1);
$this->assertIdentical($this->CakeEmail->subject(), '1');
$result = $this->CakeEmail->subject(array('something'));
$this->assertIdentical($this->CakeEmail->subject(), 'Array');
$this->assertIdentical($this->CakeEmail, $result);
$this->CakeEmail->subject('هذه رسالة بعنوان طويل مرسل للمستلم');
$expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
$this->assertIdentical($this->CakeEmail->subject(), $expected);
}
/**
* testHeaders method
*
* @return void
*/
public function testHeaders() {
$this->CakeEmail->messageId(false);
$this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
$expected = array(
'X-Something' => 'nice',
'X-Mailer' => 'CakePHP Email',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '7bit'
);
$this->assertIdentical($this->CakeEmail->getHeaders(), $expected);
$this->CakeEmail->addHeaders(array('X-Something' => 'very nice', 'X-Other' => 'cool'));
$expected = array(
'X-Something' => 'very nice',
'X-Other' => 'cool',
'X-Mailer' => 'CakePHP Email',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '7bit'
);
$this->assertIdentical($this->CakeEmail->getHeaders(), $expected);
$this->CakeEmail->from('cake@cakephp.org');
$this->assertIdentical($this->CakeEmail->getHeaders(), $expected);
$expected = array(
'From' => 'cake@cakephp.org',
'X-Something' => 'very nice',
'X-Other' => 'cool',
'X-Mailer' => 'CakePHP Email',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '7bit'
);
$this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true)), $expected);
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
$expected['From'] = 'CakePHP <cake@cakephp.org>';
$this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true)), $expected);
$this->CakeEmail->to(array('cake@cakephp.org', 'php@cakephp.org' => 'CakePHP'));
$expected = array(
'From' => 'CakePHP <cake@cakephp.org>',
'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
'X-Something' => 'very nice',
'X-Other' => 'cool',
'X-Mailer' => 'CakePHP Email',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '7bit'
);
$this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
}
/**
* testTemplate method
*
* @return void
*/
public function testTemplate() {
$this->CakeEmail->template('template', 'layout');
$expected = array('template' => 'template', 'layout' => 'layout');
$this->assertIdentical($this->CakeEmail->template(), $expected);
$this->CakeEmail->template('new_template');
$expected = array('template' => 'new_template', 'layout' => 'layout');
$this->assertIdentical($this->CakeEmail->template(), $expected);
$this->CakeEmail->template('template', null);
$expected = array('template' => 'template', 'layout' => null);
$this->assertIdentical($this->CakeEmail->template(), $expected);
$this->CakeEmail->template(null, null);
$expected = array('template' => null, 'layout' => null);
$this->assertIdentical($this->CakeEmail->template(), $expected);
}
/**
* testViewVars method
*
* @return void
*/
public function testViewVars() {
$this->assertIdentical($this->CakeEmail->viewVars(), array());
$this->CakeEmail->viewVars(array('value' => 12345));
$this->assertIdentical($this->CakeEmail->viewVars(), array('value' => 12345));
$this->CakeEmail->viewVars(array('name' => 'CakePHP'));
$this->assertIdentical($this->CakeEmail->viewVars(), array('value' => 12345, 'name' => 'CakePHP'));
$this->CakeEmail->viewVars(array('value' => 4567));
$this->assertIdentical($this->CakeEmail->viewVars(), array('value' => 4567, 'name' => 'CakePHP'));
}
/**
* testAttachments method
*
* @return void
*/
public function testAttachments() {
$this->CakeEmail->attachments(CAKE . 'basics.php');
$expected = array('basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'application/octet-stream'));
$this->assertIdentical($this->CakeEmail->attachments(), $expected);
$this->CakeEmail->attachments(array());
$this->assertIdentical($this->CakeEmail->attachments(), array());
$this->CakeEmail->attachments(array(array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
$this->CakeEmail->addAttachments(CAKE . 'bootstrap.php');
$this->CakeEmail->addAttachments(array(CAKE . 'bootstrap.php'));
$this->CakeEmail->addAttachments(array('other.txt' => CAKE . 'bootstrap.php', 'license' => CAKE . 'LICENSE.txt'));
$expected = array(
'basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'),
'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'application/octet-stream')
);
$this->assertIdentical($this->CakeEmail->attachments(), $expected);
}
/**
* testTransport method
*
* @return void
*/
public function testTransport() {
$result = $this->CakeEmail->transport('debug');
$this->assertIdentical($this->CakeEmail, $result);
$this->assertIdentical($this->CakeEmail->transport(), 'debug');
$result = $this->CakeEmail->transportClass();
$this->assertIsA($result, 'DebugTransport');
}
/**
* testConfig method
*
* @return void
*/
public function testConfig() {
$this->CakeEmail->transport('debug')->transportClass();
DebugTransport::$config = array();
$config = array('test' => 'ok', 'test2' => true);
$this->CakeEmail->config($config);
$this->assertIdentical(DebugTransport::$config, $config);
$this->assertIdentical($this->CakeEmail->config(), $config);
$this->CakeEmail->config(array());
$this->assertIdentical(DebugTransport::$config, array());
}
/**
* testSendWithContent method
*
* @return void
*/
public function testSendWithContent() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
DebugTransport::$includeAddresses = false;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
$result = $this->CakeEmail->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
$this->assertTrue($result);
$expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
$this->assertIdentical(DebugTransport::$lastEmail, $expected);
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Date: '));
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
$this->assertFalse(strpos(DebugTransport::$lastHeader, 'To: '));
DebugTransport::$includeAddresses = true;
$this->CakeEmail->send("Other body");
$this->assertIdentical(DebugTransport::$lastEmail, "Other body\r\n\r\n");
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'To: '));
}
/**
* testSendRender method
*
* @return void
*/
public function testSendRender() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
DebugTransport::$includeAddresses = true;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
$this->CakeEmail->template('default', 'default');
$result = $this->CakeEmail->send();
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the CakePHP Framework'));
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'To: '));
}
/**
* testSendRenderWithVars method
*
* @return void
*/
public function testSendRenderWithVars() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
DebugTransport::$includeAddresses = true;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
$this->CakeEmail->template('custom', 'default');
$this->CakeEmail->viewVars(array('value' => 12345));
$result = $this->CakeEmail->send();
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Here is your value: 12345'));
}
/**
* testSendMultipleMIME method
*
* @return void
*/
public function testSendMultipleMIME() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
DebugTransport::$includeAddresses = true;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->template('custom', 'default');
$this->CakeEmail->config(array());
$this->CakeEmail->viewVars(array('value' => 12345));
$this->CakeEmail->emailFormat('both');
$result = $this->CakeEmail->send();
$message = $this->CakeEmail->message();
$boundary = $this->CakeEmail->getBoundary();
$this->assertFalse(empty($boundary));
$this->assertFalse(in_array('--' . $boundary, $message));
$this->assertFalse(in_array('--' . $boundary . '--', $message));
$this->assertTrue(in_array('--alt-' . $boundary, $message));
$this->assertTrue(in_array('--alt-' . $boundary . '--', $message));
$this->CakeEmail->attachments(array('fake.php' => __FILE__));
$this->CakeEmail->send();
$message = $this->CakeEmail->message();
$boundary = $this->CakeEmail->getBoundary();
$this->assertFalse(empty($boundary));
$this->assertTrue(in_array('--' . $boundary, $message));
$this->assertTrue(in_array('--' . $boundary . '--', $message));
$this->assertTrue(in_array('--alt-' . $boundary, $message));
$this->assertTrue(in_array('--alt-' . $boundary . '--', $message));
}
/**
* testSendAttachment method
*
* @return void
*/
public function testSendAttachment() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
DebugTransport::$includeAddresses = false;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array());
$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
$this->CakeEmail->send('body');
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, "Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"basics.php\""));
$this->CakeEmail->attachments(array('my.file.txt' => CAKE . 'basics.php'));
$this->CakeEmail->send('body');
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, "Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\""));
$this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
$this->CakeEmail->send('body');
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, "Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"file.txt\""));
$this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain', 'contentId' => 'a1b1c1')));
$this->CakeEmail->send('body');
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, "Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-ID: <a1b1c1>\r\nContent-Disposition: inline; filename=\"file.txt\""));
}
/**
* testDeliver method
*
* @return void
*/
public function testDeliver() {
$instance = CakeEmail::deliver('all@cakephp.org', 'About', 'Everything ok', array('from' => 'root@cakephp.org'), false);
$this->assertIsA($instance, 'CakeEmail');
$this->assertIdentical($instance->to(), array('all@cakephp.org' => 'all@cakephp.org'));
$this->assertIdentical($instance->subject(), 'About');
$this->assertIdentical($instance->from(), array('root@cakephp.org' => 'root@cakephp.org'));
$config = array(
'from' => 'cake@cakephp.org',
'to' => 'debug@cakephp.org',
'subject' => 'Update ok',
'template' => 'custom',
'layout' => 'custom_layout',
'viewVars' => array('value' => 123),
'cc' => array('cake@cakephp.org' => 'Myself')
);
$instance = CakeEmail::deliver(null, null, array('name' => 'CakePHP'), $config, false);
$this->assertIdentical($instance->from(), array('cake@cakephp.org' => 'cake@cakephp.org'));
$this->assertIdentical($instance->to(), array('debug@cakephp.org' => 'debug@cakephp.org'));
$this->assertIdentical($instance->subject(), 'Update ok');
$this->assertIdentical($instance->template(), array('template' => 'custom', 'layout' => 'custom_layout'));
$this->assertIdentical($instance->viewVars(), array('value' => 123, 'name' => 'CakePHP'));
$this->assertIdentical($instance->cc(), array('cake@cakephp.org' => 'Myself'));
}
/**
* testMessage method
*
* @return void
*/
public function testMessage() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
DebugTransport::$includeAddresses = true;
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
$this->CakeEmail->template('default', 'default');
$this->CakeEmail->emailFormat('both');
$result = $this->CakeEmail->send();
$expected = '<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>';
$this->assertTrue((bool)strpos($this->CakeEmail->message(CakeEmail::MESSAGE_HTML), $expected));
$expected = 'This email was sent using the CakePHP Framework, http://cakephp.org.';
$this->assertTrue((bool)strpos($this->CakeEmail->message(CakeEmail::MESSAGE_TEXT), $expected));
$message = $this->CakeEmail->message();
$this->assertTrue(in_array('Content-Type: text/plain; charset=UTF-8', $message));
$this->assertTrue(in_array('Content-Type: text/html; charset=UTF-8', $message));
}
/**
* testReset method
*
* @return void
*/
public function testReset() {
$this->CakeEmail->to('cake@cakephp.org');
$this->assertIdentical($this->CakeEmail->to(), array('cake@cakephp.org' => 'cake@cakephp.org'));
$this->CakeEmail->reset();
$this->assertIdentical($this->CakeEmail->to(), array());
}
/**
* testWrap method
*
* @return void
*/
public function testWrap() {
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
$result = $this->CakeEmail->wrap($text);
$expected = array(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
''
);
$this->assertIdentical($result, $expected);
$text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
$result = $this->CakeEmail->wrap($text);
$expected = array(
'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
'amet.',
''
);
$this->assertIdentical($result, $expected);
$text = '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula pellentesque accumsan amet.<hr></p>';
$result = $this->CakeEmail->wrap($text);
$expected = array(
'<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
'pellentesque accumsan amet.<hr></p>',
''
);
$this->assertIdentical($result, $expected);
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac <a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
$result = $this->CakeEmail->wrap($text);
$expected = array(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
'<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
'nisi, vehicula pellentesque accumsan amet.',
''
);
$this->assertIdentical($result, $expected);
$text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
$result = $this->CakeEmail->wrap($text);
$expected = array(
'Lorem ipsum',
'<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
'ok</a>',
''
);
$this->assertIdentical($result, $expected);
$text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
$result = $this->CakeEmail->wrap($text);
$expected = array(
'Lorem ipsum',
'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
'ok.',
''
);
$this->assertIdentical($result, $expected);
}
}

View file

@ -0,0 +1,261 @@
<?php
/**
* SmtpTransportTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake.tests.cases.libs.email
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('CakeEmail', 'Network');
App::uses('AbstractTransport', 'Network/Email');
App::uses('SmtpTransport', 'Network/Email');
/**
* Help to test SmtpTransport
*
*/
class SmtpTestTransport extends SmtpTransport {
/**
* Helper to change the socket
*
* @param object $socket
* @return void
*/
public function setSocket(CakeSocket $socket) {
$this->_socket = $socket;
}
/**
* Helper to change the CakeEmail
*
* @param object $cakeEmail
* @return void
*/
public function setCakeEmail($cakeEmail) {
$this->_cakeEmail = $cakeEmail;
}
/**
* Disabled the socket change
*
* @return void
*/
protected function _generateSocket() {
return;
}
/**
* Magic function to call protected methods
*
* @param string $method
* @param string $args
* @return mixed
*/
public function __call($method, $args) {
$method = '_' . $method;
return $this->$method();
}
}
/**
* Test case
*
*/
class StmpProtocolTest extends CakeTestCase {
/**
* Setup
*
* @return void
*/
public function setUp() {
if (!class_exists('MockSocket')) {
$this->getMock('CakeSocket', array('read', 'write', 'connect'), array(), 'MockSocket');
}
$this->socket = new MockSocket();
$this->SmtpTransport = new SmtpTestTransport();
$this->SmtpTransport->setSocket($this->socket);
$this->SmtpTransport->config();
}
/**
* testConnectEhlo method
*
* @return void
*/
public function testConnectEhlo() {
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->SmtpTransport->connect();
}
/**
* testConnectHelo method
*
* @return void
*/
public function testConnectHelo() {
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->socket->expects($this->at(5))->method('write')->with("HELO localhost\r\n");
$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->SmtpTransport->connect();
}
/**
* testConnectFail method
*
* @expectedException Exception
* @return void
*/
public function testConnetFail() {
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->socket->expects($this->at(5))->method('write')->with("HELO localhost\r\n");
$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->SmtpTransport->connect();
}
/**
* testAuth method
*
* @return void
*/
public function testAuth() {
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("334 Login\r\n"));
$this->socket->expects($this->at(3))->method('write')->with("bWFyaw==\r\n");
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("334 Pass\r\n"));
$this->socket->expects($this->at(6))->method('write')->with("c3Rvcnk=\r\n");
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("235 OK\r\n"));
$this->SmtpTransport->config(array('username' => 'mark', 'password' => 'story'));
$this->SmtpTransport->auth();
}
/**
* testAuthNoAuth method
*
* @return void
*/
public function testAuthNoAuth() {
$this->socket->expects($this->never())->method('write')->with("AUTH LOGIN\r\n");
$this->SmtpTransport->config(array('username' => null, 'password' => null));
$this->SmtpTransport->auth();
}
/**
* testRcpt method
*
* @return void
*/
public function testRcpt() {
$email = new CakeEmail();
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->to('cake@cakephp.org', 'CakePHP');
$email->bcc('phpnut@cakephp.org');
$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(6))->method('write')->with("RCPT TO:<mark@cakephp.org>\r\n");
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(9))->method('write')->with("RCPT TO:<juan@cakephp.org>\r\n");
$this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(12))->method('write')->with("RCPT TO:<phpnut@cakephp.org>\r\n");
$this->socket->expects($this->at(13))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(14))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->setCakeEmail($email);
$this->SmtpTransport->sendRcpt();
}
/**
* testSendData method
*
* @return void
*/
public function testSendData() {
$this->getMock('CakeEmail', array('message'), array(), 'TestCakeEmail');
$email = new TestCakeEmail();
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->to('cake@cakephp.org', 'CakePHP');
$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
$email->bcc('phpnut@cakephp.org');
$email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
$email->subject('Testing SMTP');
$email->expects($this->any())->method('message')->will($this->returnValue(array('First Line', 'Second Line', '')));
$data = "From: CakePHP Test <noreply@cakephp.org>\r\n";
$data .= "To: CakePHP <cake@cakephp.org>\r\n";
$data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n";
$data .= "Bcc: phpnut@cakephp.org\r\n";
$data .= "X-Mailer: CakePHP Email Component\r\n";
$data .= "Date: " . date(DATE_RFC2822) . "\r\n";
$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
$data .= "Subject: Testing SMTP\r\n";
$data .= "Content-Type: text/plain; charset=UTF-8\r\n";
$data .= "Content-Transfer-Encoding: 7bit\r\n";
$data .= "\r\n";
$data .= "First Line\r\n";
$data .= "Second Line\r\n";
$data .= "\r\n";
$data .= "\r\n\r\n.\r\n";
$this->socket->expects($this->at(0))->method('write')->with("DATA\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("354 OK\r\n"));
$this->socket->expects($this->at(3))->method('write')->with($data);
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->setCakeEmail($email);
$this->SmtpTransport->sendData();
}
/**
* testQuit method
*
* @return void
*/
public function testQuit() {
$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
$this->SmtpTransport->disconnect();
}
}