add Cakelog to send(), transport classes now all return in same format, fixing some typos

This commit is contained in:
Ceeram 2011-08-26 02:31:18 +02:00
parent c93274a528
commit 9e0ff5ad88
8 changed files with 76 additions and 83 deletions

View file

@ -35,7 +35,7 @@ abstract class AbstractTransport {
* Send mail * Send mail
* *
* @params object $email CakeEmail * @params object $email CakeEmail
* @return boolean * @return array
*/ */
abstract public function send(CakeEmail $email); abstract public function send(CakeEmail $email);

View file

@ -582,17 +582,10 @@ class CakeEmail {
* @return array * @return array
*/ */
public function getHeaders($include = array()) { public function getHeaders($include = array()) {
$defaults = array( if ($include == array_values($include)) {
'from' => false, $include = array_fill_keys($include, true);
'sender' => false, }
'replyTo' => false, $defaults = array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), false);
'readReceipt' => false,
'returnPath' => false,
'to' => false,
'cc' => false,
'bcc' => false,
'subject' => false
);
$include += $defaults; $include += $defaults;
$headers = array(); $headers = array();
@ -904,7 +897,7 @@ class CakeEmail {
/** /**
* Send an email using the specified content, template and layout * Send an email using the specified content, template and layout
* *
* @return boolean Success * @return array
* @throws SocketException * @throws SocketException
*/ */
public function send($content = null) { public function send($content = null) {
@ -948,8 +941,11 @@ class CakeEmail {
$this->_message[] = '--' . $this->_boundary . '--'; $this->_message[] = '--' . $this->_boundary . '--';
$this->_message[] = ''; $this->_message[] = '';
} }
$contents = $this->transportClass()->send($this);
return $this->transportClass()->send($this); if (isset($this->_config['log']) && $this->_config['log']) {
CakeLog::write(LOG_DEBUG, '\nHeaders:\n' . $contents['headers'] . 'Message:\n' . $contents['message']);
}
return $contents;
} }
/** /**
@ -961,6 +957,7 @@ class CakeEmail {
* @param mixed $transportConfig String to use config from EmailConfig or array with configs * @param mixed $transportConfig String to use config from EmailConfig or array with configs
* @param boolean $send Send the email or just return the instance pre-configured * @param boolean $send Send the email or just return the instance pre-configured
* @return object Instance of CakeEmail * @return object Instance of CakeEmail
* @throws SocketException
*/ */
public static function deliver($to = null, $subject = null, $message = null, $transportConfig = 'fast', $send = true) { public static function deliver($to = null, $subject = null, $message = null, $transportConfig = 'fast', $send = true) {
$class = __CLASS__; $class = __CLASS__;
@ -1259,12 +1256,12 @@ class CakeEmail {
$View->helpers = $this->_helpers; $View->helpers = $this->_helpers;
$msg = array(); $msg = array();
list($templatePlugin, $template) = pluginSplit($this->_template, true); list($templatePlugin, $template) = pluginSplit($this->_template);
list($layoutPlugin, $layout) = pluginSplit($this->_layout, true); list($layoutPlugin, $layout) = pluginSplit($this->_layout);
if (!empty($templatePlugin)) { if ($templatePlugin) {
$View->plugin = rtrim($templatePlugin, '.'); $View->plugin = $templatePlugin;
} elseif (!empty($layoutPlugin)) { } elseif ($layoutPlugin) {
$View->plugin = rtrim($layoutPlugin, '.'); $View->plugin = $layoutPlugin;
} }
$content = implode("\n", $content); $content = implode("\n", $content);

View file

@ -29,22 +29,13 @@ class DebugTransport extends AbstractTransport {
* Send mail * Send mail
* *
* @params object $email CakeEmail * @params object $email CakeEmail
* @return boolean * @return array
*/ */
public function send(CakeEmail $email) { public function send(CakeEmail $email) {
$headers = $email->getHeaders(array( $headers = $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'));
'from' => true,
'sender' => true,
'replyTo' => true,
'readReceipt' => true,
'returnPath' => true,
'to' => true,
'cc' => true,
'bcc' => true,
'subject' => true
));
$headers = $this->_headersToString($headers); $headers = $this->_headersToString($headers);
return $headers . "\n\n" . implode((array)$email->message(), "\n"); $message = implode((array)$email->message(), "\n");
return array('headers' => $headers, 'message' => $message);
} }
} }

View file

@ -28,22 +28,27 @@ class MailTransport extends AbstractTransport {
* Send mail * Send mail
* *
* @params object $email CakeEmail * @params object $email CakeEmail
* @return boolean * @return array
*/ */
public function send(CakeEmail $email) { public function send(CakeEmail $email) {
$eol = PHP_EOL; $eol = PHP_EOL;
if (isset($this->_config['eol'])) { if (isset($this->_config['eol'])) {
$eol = $this->_config['eol']; $eol = $this->_config['eol'];
} }
$headers = $email->getHeaders(array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true)); $headers = $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'));
$to = $headers['To']; $to = $headers['To'];
unset($headers['To']); unset($headers['To']);
$header = $this->_headersToString($headers, $eol); $headers = $this->_headersToString($headers, $eol);
$message = implode($eol, $email->message()); $message = implode($eol, $email->message());
if (ini_get('safe_mode') || !isset($this->_config['additionalParameters'])) { if (ini_get('safe_mode') || !isset($this->_config['additionalParameters'])) {
return @mail($to, $email->subject(), $message, $header); if (!@mail($to, $email->subject(), $message, $headers)) {
throw new SocketException(__d('cake', 'Could not send email.'));
}
} }
return @mail($to, $email->subject(), $message, $header, $this->_config['additionalParameters']); if(!@mail($to, $email->subject(), $message, $headers, $this->_config['additionalParameters'])) {
throw new SocketException(__d('cake', 'Could not send email.'));
}
return array('headers' => $headers, 'message' => $message);
} }
} }

View file

@ -39,11 +39,18 @@ class SmtpTransport extends AbstractTransport {
*/ */
protected $_cakeEmail; protected $_cakeEmail;
/**
* Content of email to return
*
* @var string
*/
protected $_content;
/** /**
* Send mail * Send mail
* *
* @params object $email CakeEmail * @params object $email CakeEmail
* @return boolean * @return array
* @throws SocketException * @throws SocketException
*/ */
public function send(CakeEmail $email) { public function send(CakeEmail $email) {
@ -55,7 +62,7 @@ class SmtpTransport extends AbstractTransport {
$this->_sendData(); $this->_sendData();
$this->_disconnect(); $this->_disconnect();
return true; return $this->_content;
} }
/** /**
@ -158,10 +165,11 @@ class SmtpTransport extends AbstractTransport {
protected function _sendData() { protected function _sendData() {
$this->_smtpSend('DATA', '354'); $this->_smtpSend('DATA', '354');
$headers = $this->_cakeEmail->getHeaders(array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), true)); $headers = $this->_cakeEmail->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'));
$headers = $this->_headersToString($headers); $headers = $this->_headersToString($headers);
$message = implode("\r\n", $this->_cakeEmail->message()); $message = implode("\r\n", $this->_cakeEmail->message());
$this->_smtpSend($headers . "\r\n\r\n" . $message . "\r\n\r\n\r\n."); $this->_smtpSend($headers . "\r\n\r\n" . $message . "\r\n\r\n\r\n.");
$this->_content = array('headers' => $headers, 'message' => $message);
} }
/** /**

View file

@ -559,16 +559,16 @@ class CakeEmailTest extends CakeTestCase {
$this->CakeEmail->config('test'); $this->CakeEmail->config('test');
$result = $this->CakeEmail->to(); $result = $this->CakeEmail->to();
$this->assertEqual($result, $configs->test['to']); $this->assertEquals($configs->test['to'], $result);
$result = $this->CakeEmail->from(); $result = $this->CakeEmail->from();
$this->assertEqual($result, $configs->test['from']); $this->assertEquals($configs->test['from'], $result);
$result = $this->CakeEmail->subject(); $result = $this->CakeEmail->subject();
$this->assertEqual($result, $configs->test['subject']); $this->assertEquals($configs->test['subject'], $result);
$result = $this->CakeEmail->transport(); $result = $this->CakeEmail->transport();
$this->assertEqual($result, $configs->test['transport']); $this->assertEquals($configs->test['transport'], $result);
$result = $this->CakeEmail->transportClass(); $result = $this->CakeEmail->transportClass();
$this->assertTrue($result instanceof DebugTransport); $this->assertTrue($result instanceof DebugTransport);
@ -933,16 +933,16 @@ class CakeEmailTest extends CakeTestCase {
$this->CakeEmail = new CakeEmail($configs); $this->CakeEmail = new CakeEmail($configs);
$result = $this->CakeEmail->to(); $result = $this->CakeEmail->to();
$this->assertEqual($result, array($configs['to'] => $configs['to'])); $this->assertEquals(array($configs['to'] => $configs['to']), $result);
$result = $this->CakeEmail->from(); $result = $this->CakeEmail->from();
$this->assertEqual($result, $configs['from']); $this->assertEquals($configs['from'], $result);
$result = $this->CakeEmail->subject(); $result = $this->CakeEmail->subject();
$this->assertEqual($result, $configs['subject']); $this->assertEquals($configs['subject'], $result);
$result = $this->CakeEmail->transport(); $result = $this->CakeEmail->transport();
$this->assertEqual($result, $configs['transport']); $this->assertEquals($configs['transport'], $result);
$result = $this->CakeEmail->transportClass(); $result = $this->CakeEmail->transportClass();
$this->assertTrue($result instanceof DebugTransport); $this->assertTrue($result instanceof DebugTransport);
@ -968,16 +968,16 @@ class CakeEmailTest extends CakeTestCase {
$this->CakeEmail = new CakeEmail('test'); $this->CakeEmail = new CakeEmail('test');
$result = $this->CakeEmail->to(); $result = $this->CakeEmail->to();
$this->assertEqual($result, $configs->test['to']); $this->assertEquals($configs->test['to'], $result);
$result = $this->CakeEmail->from(); $result = $this->CakeEmail->from();
$this->assertEqual($result, $configs->test['from']); $this->assertEquals($configs->test['from'], $result);
$result = $this->CakeEmail->subject(); $result = $this->CakeEmail->subject();
$this->assertEqual($result, $configs->test['subject']); $this->assertEquals($configs->test['subject'], $result);
$result = $this->CakeEmail->transport(); $result = $this->CakeEmail->transport();
$this->assertEqual($result, $configs->test['transport']); $this->assertEquals($configs->test['transport'], $result);
$result = $this->CakeEmail->transportClass(); $result = $this->CakeEmail->transportClass();
$this->assertTrue($result instanceof DebugTransport); $this->assertTrue($result instanceof DebugTransport);

View file

@ -17,8 +17,6 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/ */
App::uses('CakeEmail', 'Network/Email'); App::uses('CakeEmail', 'Network/Email');
App::uses('AbstractTransport', 'Network/Email');
App::uses('DebugTransport', 'Network/Email');
/** /**
* Test case * Test case
@ -26,14 +24,6 @@ App::uses('DebugTransport', 'Network/Email');
*/ */
class DebugTransportTest extends CakeTestCase { class DebugTransportTest extends CakeTestCase {
/**
* Setup
*
* @return void
*/
public function setUp() {
$this->DebugTransport = new DebugTransport();
}
/** /**
* testSend method * testSend method
@ -42,7 +32,7 @@ class DebugTransportTest extends CakeTestCase {
*/ */
public function testSend() { public function testSend() {
$this->getMock('CakeEmail', array('message'), array(), 'DebugCakeEmail'); $this->getMock('CakeEmail', array('message'), array(), 'DebugCakeEmail');
$email = new DebugCakeEmail(); $email = new DebugCakeEmail(array('transport' => 'Debug'));
$email->from('noreply@cakephp.org', 'CakePHP Test'); $email->from('noreply@cakephp.org', 'CakePHP Test');
$email->to('cake@cakephp.org', 'CakePHP'); $email->to('cake@cakephp.org', 'CakePHP');
$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso')); $email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
@ -51,23 +41,25 @@ class DebugTransportTest extends CakeTestCase {
$email->subject('Testing Message'); $email->subject('Testing Message');
$email->expects($this->any())->method('message')->will($this->returnValue(array('First Line', 'Second Line', ''))); $email->expects($this->any())->method('message')->will($this->returnValue(array('First Line', 'Second Line', '')));
$data = "From: CakePHP Test <noreply@cakephp.org>\r\n"; $headers = "From: CakePHP Test <noreply@cakephp.org>\r\n";
$data .= "To: CakePHP <cake@cakephp.org>\r\n"; $headers .= "To: CakePHP <cake@cakephp.org>\r\n";
$data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n"; $headers .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n";
$data .= "Bcc: phpnut@cakephp.org\r\n"; $headers .= "Bcc: phpnut@cakephp.org\r\n";
$data .= "X-Mailer: CakePHP Email\r\n"; $headers .= "X-Mailer: CakePHP Email\r\n";
$data .= "Date: " . date(DATE_RFC2822) . "\r\n"; $headers .= "Date: " . date(DATE_RFC2822) . "\r\n";
$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n"; $headers .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
$data .= "Subject: Testing Message\r\n"; $headers .= "Subject: Testing Message\r\n";
$data .= "MIME-Version: 1.0\r\n"; $headers .= "MIME-Version: 1.0\r\n";
$data .= "Content-Type: text/plain; charset=UTF-8\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$data .= "Content-Transfer-Encoding: 7bit"; $headers .= "Content-Transfer-Encoding: 7bit";
$data .= "\n\n";
$data .= "First Line\n"; $data = "First Line\n";
$data .= "Second Line\n"; $data .= "Second Line\n";
$result = $this->DebugTransport->send($email); $result = $email->transportClass()->send($email);
$this->assertEquals($data, $result);
$this->assertEquals($headers, $result['headers']);
$this->assertEquals($data, $result['message']);
} }
} }

View file

@ -61,7 +61,7 @@ class SmtpTestTransport extends SmtpTransport {
* @param string $method * @param string $method
* @param string $args * @param string $args
* @return mixed * @return mixed
*/ */
public function __call($method, $args) { public function __call($method, $args) {
$method = '_' . $method; $method = '_' . $method;
return $this->$method(); return $this->$method();
@ -73,7 +73,7 @@ class SmtpTestTransport extends SmtpTransport {
* Test case * Test case
* *
*/ */
class StmpProtocolTest extends CakeTestCase { class SmtpTransportTest extends CakeTestCase {
/** /**
* Setup * Setup
@ -130,7 +130,7 @@ class StmpProtocolTest extends CakeTestCase {
* @expectedException Exception * @expectedException Exception
* @return void * @return void
*/ */
public function testConnetFail() { public function testConnectFail() {
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true)); $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(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(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));