diff --git a/cake/libs/controller/components/email.php b/cake/libs/controller/components/email.php index 8db79468a..8ba40bc4a 100644 --- a/cake/libs/controller/components/email.php +++ b/cake/libs/controller/components/email.php @@ -246,6 +246,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 * @@ -324,6 +340,17 @@ class EmailComponent extends Object{ $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 @@ class EmailComponent extends Object{ $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 @@ class EmailComponent extends Object{ $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 @@ class EmailComponent extends Object{ $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 @@ class EmailComponent extends Object{ $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; diff --git a/cake/tests/cases/libs/controller/components/email.test.php b/cake/tests/cases/libs/controller/components/email.test.php index 2c60027b0..dde095fb5 100644 --- a/cake/tests/cases/libs/controller/components/email.test.php +++ b/cake/tests/cases/libs/controller/components/email.test.php @@ -416,8 +416,6 @@ HEADBLOC; This is the body of the message This email was sent using the CakePHP Framework, http://cakephp.org. - - TEXTBLOC; $html = <<This email was sent using the CakePHP Framework

- HTMLBLOC; $this->Controller->EmailTest->sendAs = 'text'; @@ -546,6 +543,103 @@ TEXTBLOC; } +/** + * 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 = << + + + + EmailTest + + + +

Here is your value: 22091985

+

This email was sent using the CakePHP Framework

+ + +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 * @@ -715,6 +809,8 @@ TEXTBLOC; $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 @@ TEXTBLOC; $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() { diff --git a/cake/tests/test_app/views/elements/email/html/custom.ctp b/cake/tests/test_app/views/elements/email/html/custom.ctp new file mode 100644 index 000000000..b65169735 --- /dev/null +++ b/cake/tests/test_app/views/elements/email/html/custom.ctp @@ -0,0 +1,26 @@ + +

Here is your value:

\ No newline at end of file diff --git a/cake/tests/test_app/views/elements/email/text/custom.ctp b/cake/tests/test_app/views/elements/email/text/custom.ctp new file mode 100644 index 000000000..c28b6679b --- /dev/null +++ b/cake/tests/test_app/views/elements/email/text/custom.ctp @@ -0,0 +1,26 @@ + +Here is your value: \ No newline at end of file