Skip to content

Commit

Permalink
Pulling out view rendering from boundary setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 28, 2011
1 parent 34eedcc commit 53598c7
Showing 1 changed file with 58 additions and 32 deletions.
90 changes: 58 additions & 32 deletions lib/Cake/Network/Email/CakeEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -1240,6 +1244,7 @@ protected function _wrap($message) {
}
$formatted[] = trim(substr($tmpLine, 0, $lastSpace));
$tmpLine = substr($tmpLine, $lastSpace + 1);

$tmpLineLength = strlen($tmpLine);
}
}
Expand Down Expand Up @@ -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 . '"';
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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') {
Expand All @@ -1417,6 +1395,54 @@ protected function _render($content) {
return array_merge($msg, $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
*
Expand Down

0 comments on commit 53598c7

Please sign in to comment.