Browse files

Implemented rendered e-mail retrieval.

  • Loading branch information...
1 parent a9a3138 commit e5836ff4e99b86b177a65c38c10cbcd3fbb328cb @Phally Phally committed Dec 16, 2009
View
43 cake/libs/controller/components/email.php
@@ -247,6 +247,22 @@ class EmailComponent extends Object{
var $smtpError = null;
/**
+ * Contains the rendered plain text message if one was sent.
+ *
+ * @var string
+ * @access public
+ */
+ var $textMessage = null;
+
+/**
+ * Contains the rendered plain text message if one was sent.
+ *
+ * @var string
+ * @access public
+ */
+ var $htmlMessage = null;
+
+/**
* Temporary store of message header lines
*
* @var array
@@ -324,6 +340,17 @@ function send($content = null, $template = null, $layout = null) {
$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;
+ }
+ }
+
$message = $this->_wrap($content);
if ($this->template === null) {
@@ -372,6 +399,8 @@ function reset() {
$this->additionalParams = null;
$this->smtpError = null;
$this->attachments = array();
+ $this->htmlMessage = null;
+ $this->textMessage = null;
$this->__header = array();
$this->__boundary = null;
$this->__message = array();
@@ -415,7 +444,8 @@ function _render($content) {
$content = $View->element('email' . DS . 'text' . DS . $this->template, array('content' => $content), true);
$View->layoutPath = 'email' . DS . 'text';
- $content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
+ $content = explode("\n", $this->textMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
+
$msg = array_merge($msg, $content);
$msg[] = '';
@@ -426,7 +456,7 @@ function _render($content) {
$htmlContent = $View->element('email' . DS . 'html' . DS . $this->template, array('content' => $htmlContent), true);
$View->layoutPath = 'email' . DS . 'html';
- $htmlContent = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
+ $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 . '--';
@@ -452,7 +482,14 @@ function _render($content) {
$content = $View->element('email' . DS . $this->sendAs . DS . $this->template, array('content' => $content), true);
$View->layoutPath = 'email' . DS . $this->sendAs;
- $content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
+ $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);
return $msg;
View
103 cake/tests/cases/libs/controller/components/email.test.php
@@ -416,8 +416,6 @@ function testTemplates() {
This is the body of the message
This email was sent using the CakePHP Framework, http://cakephp.org.
-
-
TEXTBLOC;
$html = <<<HTMLBLOC
@@ -433,7 +431,6 @@ function testTemplates() {
<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
</body>
</html>
-
HTMLBLOC;
$this->Controller->EmailTest->sendAs = 'text';
@@ -547,6 +544,103 @@ function testSendDebug() {
}
/**
+ * testMessageRetrievalWithoutTemplate method
+ *
+ * @access public
+ * @return void
+ */
+ function testMessageRetrievalWithoutTemplate() {
+ App::build(array(
+ 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
+ ));
+
+ $this->Controller->EmailTest->to = 'postmaster@localhost';
+ $this->Controller->EmailTest->from = 'noreply@example.com';
+ $this->Controller->EmailTest->subject = 'Cake Debug Test';
+ $this->Controller->EmailTest->replyTo = 'noreply@example.com';
+ $this->Controller->EmailTest->layout = 'default';
+ $this->Controller->EmailTest->template = null;
+
+ $this->Controller->EmailTest->delivery = 'debug';
+
+ $text = $html = 'This is the body of the message';
+
+ $this->Controller->EmailTest->sendAs = 'both';
+ $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
+ $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
+ $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
+
+ $this->Controller->EmailTest->sendAs = 'text';
+ $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
+ $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
+ $this->assertNull($this->Controller->EmailTest->htmlMessage);
+
+ $this->Controller->EmailTest->sendAs = 'html';
+ $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
+ $this->assertNull($this->Controller->EmailTest->textMessage);
+ $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
+ }
+
+/**
+ * testMessageRetrievalWithTemplate method
+ *
+ * @access public
+ * @return void
+ */
+ function testMessageRetrievalWithTemplate() {
+ App::build(array(
+ 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
+ ));
+
+ $this->Controller->set('value', 22091985);
+
+ $this->Controller->EmailTest->to = 'postmaster@localhost';
+ $this->Controller->EmailTest->from = 'noreply@example.com';
+ $this->Controller->EmailTest->subject = 'Cake Debug Test';
+ $this->Controller->EmailTest->replyTo = 'noreply@example.com';
+ $this->Controller->EmailTest->layout = 'default';
+ $this->Controller->EmailTest->template = 'custom';
+
+ $this->Controller->EmailTest->delivery = 'debug';
+
+ $text = <<<TEXTBLOC
+
+Here is your value: 22091985
+This email was sent using the CakePHP Framework, http://cakephp.org.
+TEXTBLOC;
+
+ $html = <<<HTMLBLOC
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
+
+<html>
+<head>
+ <title>EmailTest</title>
+</head>
+
+<body>
+ <p>Here is your value: <b>22091985</b></p>
+ <p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
+</body>
+</html>
+HTMLBLOC;
+
+ $this->Controller->EmailTest->sendAs = 'both';
+ $this->assertTrue($this->Controller->EmailTest->send());
+ $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
+ $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
+
+ $this->Controller->EmailTest->sendAs = 'text';
+ $this->assertTrue($this->Controller->EmailTest->send());
+ $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
+ $this->assertNull($this->Controller->EmailTest->htmlMessage);
+
+ $this->Controller->EmailTest->sendAs = 'html';
+ $this->assertTrue($this->Controller->EmailTest->send());
+ $this->assertNull($this->Controller->EmailTest->textMessage);
+ $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
+ }
+
+/**
* testContentArray method
*
* @access public
@@ -715,6 +809,8 @@ function testReset() {
$this->Controller->EmailTest->delivery = 'smtp';
$this->Controller->EmailTest->smtpOptions['host'] = 'blah';
$this->Controller->EmailTest->attachments = array('attachment1', 'attachment2');
+ $this->Controller->EmailTest->textMessage = 'This is the body of the message';
+ $this->Controller->EmailTest->htmlMessage = 'This is the body of the message';
$this->assertFalse($this->Controller->EmailTest->send('Should not work'));
@@ -734,6 +830,7 @@ function testReset() {
$this->assertIdentical($this->Controller->EmailTest->getMessage(), array());
$this->assertNull($this->Controller->EmailTest->smtpError);
$this->assertIdentical($this->Controller->EmailTest->attachments, array());
+ $this->assertNull($this->Controller->EmailTest->textMessage);
}
function testPluginCustomViewClass() {
View
26 cake/tests/test_app/views/elements/email/html/custom.ctp
@@ -0,0 +1,26 @@
+<?php
+/* SVN FILE: $Id$ */
+
+/**
+ *
+ * PHP versions 4 and 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
+ * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @filesource
+ * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
+ * @package cake
+ * @subpackage cake.cake.libs.view.templates.elements.email.html
+ * @since CakePHP(tm) v 0.10.0.1076
+ * @version $Revision$
+ * @modifiedby $LastChangedBy$
+ * @lastmodified $Date$
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+?>
+<p>Here is your value: <b><?php echo $value; ?></b></p>
View
26 cake/tests/test_app/views/elements/email/text/custom.ctp
@@ -0,0 +1,26 @@
+<?php
+/* SVN FILE: $Id$ */
+
+/**
+ *
+ * PHP versions 4 and 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
+ * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @filesource
+ * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
+ * @package cake
+ * @subpackage cake.cake.libs.view.templates.elements.email.text
+ * @since CakePHP(tm) v 0.10.0.1076
+ * @version $Revision$
+ * @modifiedby $LastChangedBy$
+ * @lastmodified $Date$
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+?>
+Here is your value: <?php echo $value; ?>

0 comments on commit e5836ff

Please sign in to comment.