Skip to content

Commit

Permalink
More efficient, more readable loops in wrapText
Browse files Browse the repository at this point in the history
Add multibyte wordwrap test
phpDoc cleanup in tests
  • Loading branch information
Synchro committed Mar 4, 2015
1 parent 93c8cd0 commit 541cfec
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 34 deletions.
27 changes: 17 additions & 10 deletions class.phpmailer.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
73 changes: 49 additions & 24 deletions test/phpmailerTest.php
Expand Up @@ -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()
Expand All @@ -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!
*/
Expand Down Expand Up @@ -646,7 +646,7 @@ public function testValidate()
}

/**
* Try a plain message.
* Word-wrap an ASCII message.
*/
public function testWordWrap()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -749,7 +771,7 @@ public function testQuotedPrintable()
}

/**
* Try a plain message.
* Send an HTML message.
*/
public function testHtml()
{
Expand Down Expand Up @@ -925,7 +947,7 @@ public function testAltBodyAttachment()
}

/**
* iCal event test
* iCal event test.
*/
public function testIcal()
{
Expand Down Expand Up @@ -993,7 +1015,7 @@ public function testIcal()
}

/**
* Test sending multiple messages with separate connections
* Test sending multiple messages with separate connections.
*/
public function testMultipleSend()
{
Expand All @@ -1010,7 +1032,7 @@ public function testMultipleSend()
}

/**
* Test sending using SendMail
* Test sending using SendMail.
*/
public function testSendmailSend()
{
Expand All @@ -1024,7 +1046,7 @@ public function testSendmailSend()
}

/**
* Test sending using Qmail
* Test sending using Qmail.
*/
public function testQmailSend()
{
Expand All @@ -1043,7 +1065,7 @@ public function testQmailSend()
}

/**
* Test sending using PHP mail() function
* Test sending using PHP mail() function.
*/
public function testMailSend()
{
Expand All @@ -1060,7 +1082,7 @@ public function testMailSend()
}

/**
* Test sending an empty body
* Test sending an empty body.
*/
public function testEmptyBody()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -1119,7 +1141,7 @@ public function testDenialOfServiceAttack2()
}

/**
* Test error handling
* Test error handling.
*/
public function testError()
{
Expand All @@ -1135,7 +1157,7 @@ public function testError()
}

/**
* Test addressing
* Test addressing.
*/
public function testAddressing()
{
Expand Down Expand Up @@ -1166,7 +1188,7 @@ public function testAddressing()
}

/**
* Test address escaping
* Test address escaping.
*/
public function testAddressEscaping()
{
Expand All @@ -1181,7 +1203,7 @@ public function testAddressEscaping()
}

/**
* Test BCC-only addressing
* Test BCC-only addressing.
*/
public function testBCCAddressing()
{
Expand All @@ -1193,7 +1215,7 @@ public function testBCCAddressing()
}

/**
* Encoding and charset tests
* Encoding and charset tests.
*/
public function testEncodings()
{
Expand Down Expand Up @@ -1227,6 +1249,9 @@ public function testEncodings()
);
}

/**
* Test base-64 encoding.
*/
public function testBase64()
{
$this->Mail->Subject .= ': Base-64 encoding';
Expand All @@ -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()
{
Expand Down Expand Up @@ -1280,7 +1305,7 @@ public function testSigning()
}

/**
* DKIM Signing tests
* DKIM Signing tests.
*/
public function testDKIM()
{
Expand All @@ -1307,7 +1332,7 @@ public function testDKIM()
}

/**
* Test line break reformatting
* Test line break reformatting.
*/
public function testLineBreaks()
{
Expand All @@ -1323,7 +1348,7 @@ public function testLineBreaks()
}

/**
* Test setting and retrieving message ID
* Test setting and retrieving message ID.
*/
public function testMessageID()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 541cfec

Please sign in to comment.