diff --git a/class.phpmailer.php b/class.phpmailer.php index 7c73d3f92..69f5ff1da 100644 --- a/class.phpmailer.php +++ b/class.phpmailer.php @@ -1504,20 +1504,23 @@ public function wrapText($message, $length, $qp_mode = false) $crlflen = strlen(self::CRLF); $message = $this->fixEOL($message); + //Remove a trailing line break if (substr($message, -$lelen) == $this->LE) { $message = substr($message, 0, -$lelen); } - $line = explode($this->LE, $message); // Magic. We know fixEOL uses $LE + //Split message into lines + $lines = explode($this->LE, $message); + //Message will be rebuilt in here $message = ''; - for ($i = 0; $i < count($line); $i++) { - $line_part = explode(' ', $line[$i]); + foreach ($lines as $line) { + $words = explode(' ', $line); $buf = ''; - for ($e = 0; $e < count($line_part); $e++) { - $word = $line_part[$e]; + $firstword = true; + foreach ($words as $word) { if ($qp_mode and (strlen($word) > $length)) { $space_left = $length - strlen($buf) - $crlflen; - if ($e != 0) { + if (!$firstword) { if ($space_left > 20) { $len = $space_left; if ($is_utf8) { @@ -1559,13 +1562,17 @@ public function wrapText($message, $length, $qp_mode = false) } } else { $buf_o = $buf; - $buf .= ($e == 0) ? $word : (' ' . $word); + if (!$firstword) { + $buf .= ' '; + } + $buf .= $word; if (strlen($buf) > $length and $buf_o != '') { $message .= $buf_o . $soft_break; $buf = $word; } } + $firstword = false; } $message .= $buf . self::CRLF; } @@ -1726,10 +1733,10 @@ public function createHeader() } // Add custom headers - for ($index = 0; $index < count($this->CustomHeader); $index++) { + foreach ($this->CustomHeader as $header) { $result .= $this->headerLine( - trim($this->CustomHeader[$index][0]), - $this->encodeHeader(trim($this->CustomHeader[$index][1])) + trim($header[0]), + $this->encodeHeader(trim($header[1])) ); } if (!$this->sign_key_file) { diff --git a/test/phpmailerTest.php b/test/phpmailerTest.php index 6486fc11d..2fd66e6eb 100644 --- a/test/phpmailerTest.php +++ b/test/phpmailerTest.php @@ -304,7 +304,7 @@ public function testBootstrap() } /** - * Test CRAM-MD5 authentication + * Test CRAM-MD5 authentication. * Needs a connection to a server that supports this auth mechanism, so commented out by default */ public function testAuthCRAMMD5() @@ -326,7 +326,7 @@ public function testAuthCRAMMD5() } /** - * Test email address validation + * Test email address validation. * Test addresses obtained from http://isemail.info * Some failing cases commented out that are apparently up for debate! */ @@ -646,7 +646,7 @@ public function testValidate() } /** - * Try a plain message. + * Word-wrap an ASCII message. */ public function testWordWrap() { @@ -668,7 +668,29 @@ public function testWordWrap() } /** - * Try a plain message. + * Word-wrap a multibyte message. + */ + public function testWordWrapMultibyte() + { + $this->Mail->WordWrap = 40; + $my_body = str_repeat( + '飛兒樂 團光茫 飛兒樂 團光茫 飛兒樂 團光茫 飛兒樂 團光茫 ' . + '飛飛兒樂 團光茫兒樂 團光茫飛兒樂 團光飛兒樂 團光茫飛兒樂 團光茫兒樂 團光茫 ' . + '飛兒樂 團光茫飛兒樂 團飛兒樂 團光茫光茫飛兒樂 團光茫. ', + 10 + ); + $nBodyLen = strlen($my_body); + $my_body .= "\n\nThis is the above body length: " . $nBodyLen; + + $this->Mail->Body = $my_body; + $this->Mail->Subject .= ': Wordwrap multibyte'; + + $this->buildBody(); + $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo); + } + + /** + * Test low priority. */ public function testLowPriority() { @@ -749,7 +771,7 @@ public function testQuotedPrintable() } /** - * Try a plain message. + * Send an HTML message. */ public function testHtml() { @@ -925,7 +947,7 @@ public function testAltBodyAttachment() } /** - * iCal event test + * iCal event test. */ public function testIcal() { @@ -993,7 +1015,7 @@ public function testIcal() } /** - * Test sending multiple messages with separate connections + * Test sending multiple messages with separate connections. */ public function testMultipleSend() { @@ -1010,7 +1032,7 @@ public function testMultipleSend() } /** - * Test sending using SendMail + * Test sending using SendMail. */ public function testSendmailSend() { @@ -1024,7 +1046,7 @@ public function testSendmailSend() } /** - * Test sending using Qmail + * Test sending using Qmail. */ public function testQmailSend() { @@ -1043,7 +1065,7 @@ public function testQmailSend() } /** - * Test sending using PHP mail() function + * Test sending using PHP mail() function. */ public function testMailSend() { @@ -1060,7 +1082,7 @@ public function testMailSend() } /** - * Test sending an empty body + * Test sending an empty body. */ public function testEmptyBody() { @@ -1075,7 +1097,7 @@ public function testEmptyBody() } /** - * Test keepalive (sending multiple messages in a single connection) + * Test keepalive (sending multiple messages in a single connection). */ public function testSmtpKeepAlive() { @@ -1119,7 +1141,7 @@ public function testDenialOfServiceAttack2() } /** - * Test error handling + * Test error handling. */ public function testError() { @@ -1135,7 +1157,7 @@ public function testError() } /** - * Test addressing + * Test addressing. */ public function testAddressing() { @@ -1166,7 +1188,7 @@ public function testAddressing() } /** - * Test address escaping + * Test address escaping. */ public function testAddressEscaping() { @@ -1181,7 +1203,7 @@ public function testAddressEscaping() } /** - * Test BCC-only addressing + * Test BCC-only addressing. */ public function testBCCAddressing() { @@ -1193,7 +1215,7 @@ public function testBCCAddressing() } /** - * Encoding and charset tests + * Encoding and charset tests. */ public function testEncodings() { @@ -1227,6 +1249,9 @@ public function testEncodings() ); } + /** + * Test base-64 encoding. + */ public function testBase64() { $this->Mail->Subject .= ': Base-64 encoding'; @@ -1235,7 +1260,7 @@ public function testBase64() $this->assertTrue($this->Mail->send(), 'Base64 encoding failed'); } /** - * S/MIME Signing tests + * S/MIME Signing tests. */ public function testSigning() { @@ -1280,7 +1305,7 @@ public function testSigning() } /** - * DKIM Signing tests + * DKIM Signing tests. */ public function testDKIM() { @@ -1307,7 +1332,7 @@ public function testDKIM() } /** - * Test line break reformatting + * Test line break reformatting. */ public function testLineBreaks() { @@ -1323,7 +1348,7 @@ public function testLineBreaks() } /** - * Test setting and retrieving message ID + * Test setting and retrieving message ID. */ public function testMessageID() { @@ -1337,7 +1362,7 @@ public function testMessageID() } /** - * Miscellaneous calls to improve test coverage and some small tests + * Miscellaneous calls to improve test coverage and some small tests. */ public function testMiscellaneous() { @@ -1379,7 +1404,7 @@ public function testMiscellaneous() } /** - * Use a fake POP3 server to test POP-before-SMTP auth + * Use a fake POP3 server to test POP-before-SMTP auth. * With a known-good login */ public function testPopBeforeSmtpGood() @@ -1400,7 +1425,7 @@ public function testPopBeforeSmtpGood() } /** - * Use a fake POP3 server to test POP-before-SMTP auth + * Use a fake POP3 server to test POP-before-SMTP auth. * With a known-bad login */ public function testPopBeforeSmtpBad()