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
*
* @params object $email CakeEmail
* @return boolean
* @return array
*/
abstract public function send(CakeEmail $email);

View file

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

View file

@ -29,22 +29,13 @@ class DebugTransport extends AbstractTransport {
* Send mail
*
* @params object $email CakeEmail
* @return boolean
* @return array
*/
public function send(CakeEmail $email) {
$headers = $email->getHeaders(array(
'from' => true,
'sender' => true,
'replyTo' => true,
'readReceipt' => true,
'returnPath' => true,
'to' => true,
'cc' => true,
'bcc' => true,
'subject' => true
));
$headers = $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'));
$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
*
* @params object $email CakeEmail
* @return boolean
* @return array
*/
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));
$headers = $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'));
$to = $headers['To'];
unset($headers['To']);
$header = $this->_headersToString($headers, $eol);
$headers = $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);
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;
/**
* Content of email to return
*
* @var string
*/
protected $_content;
/**
* Send mail
*
* @params object $email CakeEmail
* @return boolean
* @return array
* @throws SocketException
*/
public function send(CakeEmail $email) {
@ -55,7 +62,7 @@ class SmtpTransport extends AbstractTransport {
$this->_sendData();
$this->_disconnect();
return true;
return $this->_content;
}
/**
@ -158,10 +165,11 @@ class SmtpTransport extends AbstractTransport {
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->_cakeEmail->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'));
$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.");
$this->_content = array('headers' => $headers, 'message' => $message);
}
/**

View file

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

View file

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