Skip to content
Permalink
Browse files

enforce a better email wrapping to send valid emails

  • Loading branch information...
dereuromark committed May 23, 2013
1 parent 11e428b commit 75b5a323e4acce51c9e3637e1152380f4b5af199
@@ -1248,6 +1248,7 @@ protected function _wrap($message, $wrapLength = CakeEmail::LINE_LENGTH_MUST) {
$message = str_replace(array("\r\n", "\r"), "\n", $message);
$lines = explode("\n", $message);
$formatted = array();
$cut = ($wrapLength == CakeEmail::LINE_LENGTH_MUST) ? true : false;
foreach ($lines as $line) {
if (empty($line)) {
@@ -1261,7 +1262,7 @@ protected function _wrap($message, $wrapLength = CakeEmail::LINE_LENGTH_MUST) {
if (!preg_match('/<[a-z]+.*>/i', $line)) {
$formatted = array_merge(
$formatted,
explode("\n", wordwrap($line, $wrapLength, "\n"))
explode("\n", wordwrap($line, $wrapLength, "\n", $cut))
);
continue;
}
@@ -1280,7 +1281,10 @@ protected function _wrap($message, $wrapLength = CakeEmail::LINE_LENGTH_MUST) {
$tmpLineLength += $tagLength;
} else {
if ($tmpLineLength > 0) {
$formatted[] = trim($tmpLine);
$formatted = array_merge(
$formatted,
explode("\n", wordwrap(trim($tmpLine), $wrapLength, "\n", $cut))
);
$tmpLine = '';
$tmpLineLength = 0;
}
@@ -1099,10 +1099,7 @@ public function testSendRenderWithHTML() {
$result = $this->CakeEmail->send();
$this->assertTextContains('<h1>HTML Ipsum Presents</h1>', $result['message']);
$lines = explode("\n", $result['message']);
foreach ($lines as $line) {
$this->assertTrue(strlen($line) <= CakeEmail::LINE_LENGTH_MUST);
}
$this->assertLineLengths($result['message']);
}
/**
@@ -1876,35 +1873,64 @@ public function testWrapLongLine() {
$this->CakeEmail->subject('Wordwrap Test');
$this->CakeEmail->config(array('empty'));
$result = $this->CakeEmail->send($message);
$expected = "<a\r\n" . 'href="http://cakephp.org">' . str_repeat('x', CakeEmail::LINE_LENGTH_MUST) . "\r\n</a>\r\n\r\n";
$expected = "<a\r\n" . 'href="http://cakephp.org">' . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - 26) . "\r\n" .
str_repeat('x', 26) . "\r\n</a>\r\n\r\n";
$this->assertEquals($expected, $result['message']);
$this->assertLineLengths($result['message']);
$str1 = "a ";
$str2 = " b";
$len = strlen($str1) + strlen($str2);
$message = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $len - 1) . $str2;
$length = strlen($str1) + strlen($str2);
$message = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length - 1) . $str2;
$result = $this->CakeEmail->send($message);
$expected = "{$message}\r\n\r\n";
$this->assertEquals($expected, $result['message']);
$this->assertLineLengths($result['message']);
$message = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $len) . $str2;
$message = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length) . $str2;
$result = $this->CakeEmail->send($message);
$expected = "{$message}\r\n\r\n";
$this->assertEquals($expected, $result['message']);
$this->assertLineLengths($result['message']);
$message = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $len + 1) . $str2;
$message = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length + 1) . $str2;
$result = $this->CakeEmail->send($message);
$expected = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $len + 1) . sprintf("\r\n%s\r\n\r\n", trim($str2));
$expected = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length + 1) . sprintf("\r\n%s\r\n\r\n", trim($str2));
$this->assertEquals($expected, $result['message']);
$this->assertLineLengths($result['message']);
}
public function testWrapWithTagsAcrossLines() {
$str = <<<HTML
<table>
<th align="right" valign="top"
style="font-weight: bold">The tag is across multiple lines</th>
</table>
HTML;
$length = strlen($str);
$message = $str . str_repeat('x', CakeEmail::LINE_LENGTH_MUST + 1);
$this->CakeEmail->reset();
$this->CakeEmail->transport('Debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->subject('Wordwrap Test');
$this->CakeEmail->config(array('empty'));
$result = $this->CakeEmail->send($message);
$message = str_replace("\r\n", "\n", substr($message, 0, -9));
$message = str_replace("\n", "\r\n", $message);
$expected = "{$message}\r\nxxxxxxxxx\r\n\r\n";
$this->assertEquals($expected, $result['message']);
$this->assertLineLengths($result['message']);
}
public function testWrapIncludeLessThanSign() {
$str = 'foo<bar';
$len = strlen($str);
$message = $str . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $len + 1);
$length = strlen($str);
$message = $str . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length + 1);
$this->CakeEmail->reset();
$this->CakeEmail->transport('Debug');
@@ -1913,8 +1939,10 @@ public function testWrapIncludeLessThanSign() {
$this->CakeEmail->subject('Wordwrap Test');
$this->CakeEmail->config(array('empty'));
$result = $this->CakeEmail->send($message);
$expected = "{$message}\r\n\r\n";
$message = substr($message, 0, -1);
$expected = "{$message}\r\nx\r\n\r\n";
$this->assertEquals($expected, $result['message']);
$this->assertLineLengths($result['message']);
}
public function testWrapForJapaneseEncoding() {
@@ -1934,4 +1962,19 @@ public function testWrapForJapaneseEncoding() {
$expected = "{$message}\r\n\r\n";
$this->assertEquals($expected, $result['message']);
}
/**
* CakeEmailTest::assertLineLengths()
*
* @param string $message
* @return void
*/
public function assertLineLengths($message) {
$lines = explode("\r\n", $message);
foreach ($lines as $line) {
$this->assertTrue(strlen($line) <= CakeEmail::LINE_LENGTH_MUST,
__d('cake_dev', 'Line length exceeds the max. limit of %s', 'CakeEmail::LINE_LENGTH_MUST'));
}
}
}
@@ -5,4 +5,8 @@
width: 300px;
height: 80px;
}</code></pre>
<p>Some more <b>Bold</b> test.</p>
<table>
<th align="right" valign="top"
style="font-weight: bold">The tag is across multiple lines - <?php echo str_repeat('x', CakeEmail::LINE_LENGTH_MUST); ?></th>
</table>
<p>Some more <?php echo str_repeat('x', CakeEmail::LINE_LENGTH_MUST); ?> <b>Bold</b> test.</p>

0 comments on commit 75b5a32

Please sign in to comment.
You can’t perform that action at this time.