Skip to content

Commit

Permalink
Use multibyte text in tests
Browse files Browse the repository at this point in the history
Add explicit test for UTF-8 with embedded images, see PHPMailer#279 and PHPMailer#377
Add ISO-8859-1 test
  • Loading branch information
Synchro committed Mar 25, 2015
1 parent 947415c commit 50eac87
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 13 deletions.
2 changes: 1 addition & 1 deletion class.smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ public function data($msg_data)
if ($in_headers and $line == '') {
$in_headers = false;
}
//We need to break this line up into several smaller lines
//Break this line up into several smaller lines if it's too long
//Micro-optimisation: isset($str[$len]) is faster than (strlen($str) > $len),
while (isset($line[self::MAX_LINE_LENGTH])) {
//Working backwards, try to find a space within the last MAX_LINE_LENGTH chars of the line to break on
Expand Down
16 changes: 8 additions & 8 deletions examples/contents.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>PHPMailer Test</title>
</head>
<body>
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<h1>This is a test of PHPMailer.</h1>
<div align="center">
<a href="https://github.com/PHPMailer/PHPMailer/"><img src="images/phpmailer.png" height="90" width="340" alt="PHPMailer rocks"></a>
</div>
<p>This example uses <strong>HTML</strong>.</p>
<p>The PHPMailer image at the top has been embedded automatically.</p>
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<h1>This is a test of PHPMailer.</h1>
<div align="center">
<a href="https://github.com/PHPMailer/PHPMailer/"><img src="images/phpmailer.png" height="90" width="340" alt="PHPMailer rocks"></a>
</div>
<p>This example uses <strong>HTML</strong>.</p>
<p>ISO-8859-1 text: éèîüçÅñæß</p>
</div>
</body>
</html>
20 changes: 20 additions & 0 deletions examples/contentsutf8.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHPMailer Test</title>
</head>
<body>
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<h1>This is a test of PHPMailer.</h1>
<div align="center">
<a href="https://github.com/PHPMailer/PHPMailer/"><img src="images/phpmailer.png" height="90" width="340" alt="PHPMailer rocks"></a>
</div>
<p>This example uses <strong>HTML</strong>.</p>
<p>Chinese text: 郵件內容為空</p>
<p>Russian text: Пустое тело сообщения</p>
<p>Armenian text: Հաղորդագրությունը դատարկ է</p>
<p>Czech text: Prázdné tělo zprávy</p>
</div>
</body>
</html>
68 changes: 64 additions & 4 deletions test/phpmailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function buildBody()
$eol = "<br>\r\n";
$bullet_start = '<li>';
$bullet_end = "</li>\r\n";
$list_start = '<ul>\r\n';
$list_start = "<ul>\r\n";
$list_end = "</ul>\r\n";
} else {
$eol = "\r\n";
Expand All @@ -166,6 +166,7 @@ public function buildBody()
$ReportBody .= '---------------------' . $eol;
$ReportBody .= 'phpmailer version: ' . $this->Mail->Version . $eol;
$ReportBody .= 'Content Type: ' . $this->Mail->ContentType . $eol;
$ReportBody .= 'CharSet: ' . $this->Mail->CharSet . $eol;

if (strlen($this->Mail->Host) > 0) {
$ReportBody .= 'Host: ' . $this->Mail->Host . $eol;
Expand Down Expand Up @@ -210,7 +211,7 @@ public function buildBody()
}

// Re-attach the original body
$this->Mail->Body .= $eol . $eol . $ReportBody;
$this->Mail->Body .= $eol . $ReportBody;
}

/**
Expand Down Expand Up @@ -800,6 +801,29 @@ public function testHtml()
$this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
}

/**
* Send a message containing ISO-8859-1 text.
*/
public function testHtmlIso8859()
{
$this->Mail->isHTML(false);
$this->Mail->Subject .= ": ISO-8859-1 HTML";
$this->Mail->CharSet = 'iso-8859-1';

//This file is in ISO-8859-1 charset
//Needs to be external because this file is in UTF-8
$content = file_get_contents('../examples/contents.html');
//Make sure it really is in ISO-8859-1!
$this->Mail->Body =
mb_convert_encoding(
$content,
"ISO-8859-1",
mb_detect_encoding($content, "UTF-8, ISO-8859-1, ISO-8859-15", true)
);
$this->buildBody();
$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
}

/**
* Send a message containing multilingual UTF-8 text.
*/
Expand All @@ -812,6 +836,7 @@ public function testHtmlUtf8()
$this->Mail->Body = <<<EOT
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HTML email test</title>
</head>
<body>
Expand All @@ -828,6 +853,41 @@ public function testHtmlUtf8()
$this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
}

/**
* Send a message containing multilingual UTF-8 text with an embedded image.
*/
public function testUtf8WithEmbeddedImage()
{
$this->Mail->isHTML(true);
$this->Mail->Subject .= ": UTF-8 with embedded image";
$this->Mail->CharSet = 'UTF-8';

$this->Mail->Body = <<<EOT
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HTML email test</title>
</head>
<body>
<p>Chinese text: 郵件內容為空</p>
<p>Russian text: Пустое тело сообщения</p>
<p>Armenian text: Հաղորդագրությունը դատարկ է</p>
<p>Czech text: Prázdné tělo zprávy</p>
Embedded Image: <img alt="phpmailer" src="cid:my-attach">
</body>
</html>
EOT;
$this->Mail->addEmbeddedImage(
'../examples/images/phpmailer.png',
'my-attach',
'phpmailer.png',
'base64',
'image/png'
);
$this->buildBody();
$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
}

/**
* Send a message containing multilingual UTF-8 text.
*/
Expand All @@ -854,7 +914,7 @@ public function testPlainUtf8()
*/
public function testMsgHTML()
{
$message = file_get_contents('../examples/contents.html');
$message = file_get_contents('../examples/contentsutf8.html');
$this->Mail->CharSet = 'utf-8';
$this->Mail->Body = '';
$this->Mail->AltBody = '';
Expand Down Expand Up @@ -904,7 +964,7 @@ public function testHTMLAttachment()
public function testEmbeddedImage()
{
$this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
'Here is an image!</a>';
'Here is an image!';
$this->Mail->Subject .= ': Embedded Image';
$this->Mail->isHTML(true);

Expand Down

0 comments on commit 50eac87

Please sign in to comment.