Skip to content

Commit

Permalink
Fixing wrap for html mode in e-mails. Fixes #663
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jun 21, 2010
1 parent 2a4489c commit 26d20b6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
17 changes: 13 additions & 4 deletions cake/libs/controller/components/email.php
Expand Up @@ -356,7 +356,11 @@ function send($content = null, $template = null, $layout = null) {
}
}

$message = $this->_wrap($content);
if ($this->sendAs === 'text') {
$message = $this->_wrap($content);
} else {
$message = $this->_wrap($content, 998);
}

if ($this->template === null) {
$message = $this->_formatMessage($message);
Expand Down Expand Up @@ -676,10 +680,11 @@ function _findFiles($attachment) {
* Wrap the message using EmailComponent::$lineLength
*
* @param string $message Message to wrap
* @param integer $lineLength Max length of line
* @return array Wrapped message
* @access private
* @access protected
*/
function _wrap($message) {
function _wrap($message, $lineLength = null) {
$message = $this->_strip($message, true);
$message = str_replace(array("\r\n","\r"), "\n", $message);
$lines = explode("\n", $message);
Expand All @@ -690,11 +695,15 @@ function _wrap($message) {
$this->lineLength = $this->_lineLength;
}

if (!$lineLength) {
$lineLength = $this->lineLength;
}

foreach ($lines as $line) {
if (substr($line, 0, 1) == '.') {
$line = '.' . $line;
}
$formatted = array_merge($formatted, explode("\n", wordwrap($line, $this->lineLength, "\n", true)));
$formatted = array_merge($formatted, explode("\n", wordwrap($line, $lineLength, "\n", true)));
}
$formatted[] = '';
return $formatted;
Expand Down
41 changes: 41 additions & 0 deletions cake/tests/cases/libs/controller/components/email.test.php
Expand Up @@ -109,6 +109,16 @@ function getMessage() {
return $this->__message;
}

/**
* Convenience getter for testing.
*
* @access protected
* @return string
*/
function _getMessage() {
return $this->__message;
}

/**
* Convenience method for testing.
*
Expand Down Expand Up @@ -1078,4 +1088,35 @@ function testMessageId() {
$this->assertNoPattern('/Message-ID:/', $result);
}

/**
* testSendMessage method
*
* @access public
* @return void
*/
function testSendMessage() {
$this->Controller->EmailTest->delivery = 'getMessage';
$this->Controller->EmailTest->lineLength = 70;

$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
$this->Controller->EmailTest->sendAs = 'text';
$result = $this->Controller->EmailTest->send($text);
$expected = array(
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do',
'eiusmod tempor incididunt ut labore et dolore magna aliqua.',
'',
''
);
$this->assertEqual($expected, $result);

$text = 'Lorem ipsum dolor sit amet, <b>consectetur</b> adipisicing elit, sed do <span>eiusmod tempor</span> incididunt ut labore et dolore magna aliqua.';
$this->Controller->EmailTest->sendAs = 'html';
$result = $this->Controller->EmailTest->send($text);
$expected = array(
$text,
'',
''
);
$this->assertEqual($expected, $result);
}
}

0 comments on commit 26d20b6

Please sign in to comment.