enforce a better email wrapping to send valid emails

This commit is contained in:
euromark 2013-05-23 18:34:39 +02:00
parent 11e428bca3
commit 75b5a323e4
3 changed files with 69 additions and 18 deletions

View file

@ -1248,6 +1248,7 @@ class CakeEmail {
$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 @@ class CakeEmail {
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 @@ class CakeEmail {
$tmpLineLength += $tagLength;
} else {
if ($tmpLineLength > 0) {
$formatted[] = trim($tmpLine);
$formatted = array_merge(
$formatted,
explode("\n", wordwrap(trim($tmpLine), $wrapLength, "\n", $cut))
);
$tmpLine = '';
$tmpLineLength = 0;
}

View file

@ -1099,10 +1099,7 @@ class CakeEmailTest extends CakeTestCase {
$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,45 @@ class CakeEmailTest extends CakeTestCase {
$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 testWrapIncludeLessThanSign() {
$str = 'foo<bar';
$len = strlen($str);
$message = $str . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $len + 1);
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');
@ -1913,8 +1920,29 @@ class CakeEmailTest extends CakeTestCase {
$this->CakeEmail->subject('Wordwrap Test');
$this->CakeEmail->config(array('empty'));
$result = $this->CakeEmail->send($message);
$expected = "{$message}\r\n\r\n";
$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';
$length = strlen($str);
$message = $str . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length + 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 = 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 @@ class CakeEmailTest extends CakeTestCase {
$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'));
}
}
}

View file

@ -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>