Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented the support to use helpers in CakeEmail. Fixes #1754

  • Loading branch information...
commit d220ee5578304b38110e45220f18dad7a478e78c 1 parent b4a7803
@jrbasso jrbasso authored
View
1  lib/Cake/Controller/Component/EmailComponent.php
@@ -332,6 +332,7 @@ public function send($content = null, $template = null, $layout = null) {
}
$lib->subject($this->subject)->messageID($this->messageId);
+ $lib->helpers($this->_controller->helpers);
$headers = array('X-Mailer' => $this->xMailer);
foreach ($this->headers as $key => $value) {
View
23 lib/Cake/Network/Email/CakeEmail.php
@@ -182,6 +182,13 @@ class CakeEmail {
protected $_viewVars = array();
/**
+ * Helpers to be used in the render
+ *
+ * @var array
+ */
+ protected $_helpers = array();
+
+/**
* Text message
*
* @var string
@@ -716,6 +723,20 @@ public function viewVars($viewVars = null) {
}
/**
+ * Helpers to be used in render
+ *
+ * @param array $helpers
+ * @return mixed
+ */
+ public function helpers($helpers = null) {
+ if ($helpers === null) {
+ return $this->_helpers;
+ }
+ $this->_helpers = (array)$helpers;
+ return $this;
+ }
+
+/**
* Email format
*
* @param string $format
@@ -1057,6 +1078,7 @@ public function reset() {
$this->_template = '';
$this->_viewRender = 'View';
$this->_viewVars = array();
+ $this->_helpers = array();
$this->_textMessage = '';
$this->_htmlMessage = '';
$this->_message = '';
@@ -1260,6 +1282,7 @@ protected function _render($content) {
$View = new $viewClass(null);
$View->viewVars = $this->_viewVars;
+ $View->helpers = $this->_helpers;
$msg = array();
list($templatePlugin, $template) = pluginSplit($this->_template, true);
View
30 lib/Cake/Test/Case/Controller/Component/EmailComponentTest.php
@@ -501,6 +501,7 @@ public function testMessageRetrievalWithTemplate() {
<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>
@@ -523,6 +524,35 @@ public function testMessageRetrievalWithTemplate() {
}
/**
+ * testMessageRetrievalWithHelper method
+ *
+ * @access public
+ * @return void
+ */
+ public function testMessageRetrievalWithHelper() {
+ App::build(array(
+ 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
+ ));
+
+ $timestamp = time();
+ $this->Controller->set('time', $timestamp);
+ $this->Controller->set('title_for_layout', 'EmailTest');
+ $this->Controller->helpers = array('Time');
+
+ $this->Controller->EmailTest->to = 'postmaster@example.com';
+ $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_helper';
+ $this->Controller->EmailTest->sendAs = 'text';
+ $this->Controller->EmailTest->delivery = 'DebugComp';
+
+ $this->assertTrue($this->Controller->EmailTest->send());
+ $this->assertTrue((bool)strpos($this->Controller->EmailTest->textMessage, 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
+ }
+
+/**
* testContentArray method
*
* @access public
View
23 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -617,6 +617,29 @@ public function testSendRenderWithVars() {
}
/**
+ * testSendRenderWithHelpers method
+ *
+ * @return void
+ */
+ public function testSendRenderWithHelpers() {
+ $this->CakeEmail->reset();
+ $this->CakeEmail->transport('debug');
+ DebugTransport::$includeAddresses = true;
+
+ $timestamp = time();
+ $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->template('custom_helper', 'default');
+ $this->CakeEmail->viewVars(array('time' => $timestamp));
+ $this->CakeEmail->helpers(array('Time'));
+ $result = $this->CakeEmail->send();
+
+ $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
+ }
+
+/**
* testSendRenderPlugin method
*
* @return void
View
19 lib/Cake/Test/test_app/View/Emails/text/custom_helper.ctp
@@ -0,0 +1,19 @@
+<?php
+/**
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @package cake.libs.view.templates.pages
+ * @since CakePHP(tm) v 2.0
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+?>
+Right now: <?php echo $this->Time->toAtom($time); ?>
Please sign in to comment.
Something went wrong with that request. Please try again.