Permalink
Browse files

Pulling out view rendering from boundary setting.

  • Loading branch information...
1 parent 34eedcc commit 53598c722c4fe680e8edd735373ad541d8511961 @markstory markstory committed Dec 27, 2011
Showing with 58 additions and 32 deletions.
  1. +58 −32 lib/Cake/Network/Email/CakeEmail.php
View
90 lib/Cake/Network/Email/CakeEmail.php
@@ -976,6 +976,8 @@ public function send($content = null) {
$this->_createBoundary();
$message = $this->_wrap($content);
+ // two methods doing similar things seems silly.
+ // both handle attachments.
if (empty($this->_template)) {
$message = $this->_formatMessage($message);
} else {
@@ -984,13 +986,15 @@ public function send($content = null) {
$message[] = '';
$this->_message = $message;
+ // should be part of a compose method.
if (!empty($this->_attachments)) {
$this->_attachFiles();
$this->_message[] = '';
$this->_message[] = '--' . $this->_boundary . '--';
$this->_message[] = '';
}
+
$contents = $this->transportClass()->send($this);
if (!empty($this->_config['log'])) {
$level = LOG_DEBUG;
@@ -1240,6 +1244,7 @@ protected function _wrap($message) {
}
$formatted[] = trim(substr($tmpLine, 0, $lastSpace));
$tmpLine = substr($tmpLine, $lastSpace + 1);
+
$tmpLineLength = strlen($tmpLine);
}
}
@@ -1327,31 +1332,11 @@ protected function _formatMessage($message) {
* @return array Email ready to be sent
*/
protected function _render($content) {
- $viewClass = $this->_viewRender;
-
- if ($viewClass !== 'View') {
- list($plugin, $viewClass) = pluginSplit($viewClass, true);
- $viewClass .= 'View';
- App::uses($viewClass, $plugin . 'View');
- }
-
- $View = new $viewClass(null);
- $View->viewVars = $this->_viewVars;
- $View->helpers = $this->_helpers;
- $msg = array();
-
- 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);
+ $rendered = $this->_renderTemplates($content);
+ $msg = array();
if ($this->_emailFormat === 'both') {
- $originalContent = $content;
if (!empty($this->_attachments)) {
$msg[] = '--' . $this->_boundary;
$msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
@@ -1362,9 +1347,7 @@ protected function _render($content) {
$msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
$msg[] = '';
- $View->viewPath = $View->layoutPath = 'Emails' . DS . 'text';
- $View->viewVars['content'] = $originalContent;
- $this->_textMessage = str_replace(array("\r\n", "\r"), "\n", $View->render($template, $layout));
+ $this->_textMessage = $rendered['text'];
$content = explode("\n", $this->_textMessage);
$msg = array_merge($msg, $content);
@@ -1374,10 +1357,7 @@ protected function _render($content) {
$msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
$msg[] = '';
- $View->viewPath = $View->layoutPath = 'Emails' . DS . 'html';
- $View->viewVars['content'] = $originalContent;
- $View->hasRendered = false;
- $this->_htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->render($template, $layout));
+ $this->_htmlMessage = $rendered['html'];
$content = explode("\n", $this->_htmlMessage);
$msg = array_merge($msg, $content);
@@ -1403,9 +1383,7 @@ protected function _render($content) {
}
}
- $View->viewPath = $View->layoutPath = 'Emails' . DS . $this->_emailFormat;
- $View->viewVars['content'] = $content;
- $rendered = $this->_encodeString($View->render($template, $layout), $this->charset);
+ $rendered = $this->_encodeString($rendered[$this->_emailFormat], $this->charset);
$content = explode("\n", $rendered);
if ($this->_emailFormat === 'html') {
@@ -1418,6 +1396,54 @@ protected function _render($content) {
}
/**
+ * Build and set all the view properties needed to render the templated emails.
+ * Returns false if the email is not templated.
+ *
+ * @param string $content The content passed in from send() in most cases.
+ * @return array The rendered content with html and text keys.
+ */
+ public function _renderTemplates($content) {
+ if (empty($this->_template)) {
+ return false;
+ }
+ $viewClass = $this->_viewRender;
+ if ($viewClass !== 'View') {
+ list($plugin, $viewClass) = pluginSplit($viewClass, true);
+ $viewClass .= 'View';
+ App::uses($viewClass, $plugin . 'View');
+ }
+
+ $View = new $viewClass(null);
+ $View->viewVars = $this->_viewVars;
+ $View->helpers = $this->_helpers;
+
+ list($templatePlugin, $template) = pluginSplit($this->_template);
+ list($layoutPlugin, $layout) = pluginSplit($this->_layout);
+ if ($templatePlugin) {
+ $View->plugin = $templatePlugin;
+ } elseif ($layoutPlugin) {
+ $View->plugin = $layoutPlugin;
+ }
+
+ $types = array($this->_emailFormat);
+ if ($this->_emailFormat == 'both') {
+ $types = array('html', 'text');
+ }
+
+ $rendered = array();
+ foreach ($types as $type) {
+ $View->set('content', $content);
+ $View->hasRendered = false;
+ $View->viewPath = $View->layoutPath = 'Emails' . DS . $type;
+
+ $render = $View->render($template, $layout);
+ $render = str_replace(array("\r\n", "\r"), "\n", $render);
+ $rendered[$type] = $render;
+ }
+ return $rendered;
+ }
+
+/**
* Return the Content-Transfer Encoding value based on the set charset
*
* @return void

0 comments on commit 53598c7

Please sign in to comment.