diff --git a/lib/Cake/Network/CakeEmail.php b/lib/Cake/Network/CakeEmail.php index a8f42861920..c03b121be73 100644 --- a/lib/Cake/Network/CakeEmail.php +++ b/lib/Cake/Network/CakeEmail.php @@ -52,6 +52,20 @@ class CakeEmail { */ const LINE_LENGTH_MUST = 998; +/** + * Type of message - HTML + * + * @constant MESSAGE_HTML + */ + const MESSAGE_HTML = 'html'; + +/** + * Type of message - TEXT + * + * @constant MESSAGE_TEXT + */ + const MESSAGE_TEXT = 'text'; + /** * Recipient of the email * @@ -792,9 +806,16 @@ public function addAttachments($attachments) { /** * Get generated message (used by transport classes) * - * @return array + * @param mixed $type Use MESSAGE_* constants or null to return the full message as array + * @return mixed String if have type, array if type is null */ - public function message() { + public function message($type = null) { + switch ($type) { + case self::MESSAGE_HTML: + return $this->_htmlMessage; + case self::MESSAGE_TEXT: + return $this->_textMessage; + } return $this->_message; } diff --git a/lib/Cake/tests/Case/Network/CakeEmailTest.php b/lib/Cake/tests/Case/Network/CakeEmailTest.php index 645ac864068..7149c0c5b77 100644 --- a/lib/Cake/tests/Case/Network/CakeEmailTest.php +++ b/lib/Cake/tests/Case/Network/CakeEmailTest.php @@ -539,6 +539,35 @@ public function testSendRenderWithVars() { $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Here is your value: 12345')); } +/** + * 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->layout('default', 'default'); + $this->CakeEmail->emailFormat('both'); + $result = $this->CakeEmail->send(); + + $expected = '

This email was sent using the CakePHP Framework

'; + $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 *