From c15920ece2849dcebcd19820adf83141e8aa4163 Mon Sep 17 00:00:00 2001 From: Synchro Date: Thu, 28 Feb 2013 14:21:00 +0100 Subject: [PATCH] Make MsgHTML() always overwrite AltBody, fixes #28 Break out html to text conversion to a method so it can be overridden easily and use it internally, fixes #29 --- class.phpmailer.php | 17 +++++++---- test/phpmailerTest.php | 69 +++++++++++++++++++++++++++++++++++------- 2 files changed, 69 insertions(+), 17 deletions(-) diff --git a/class.phpmailer.php b/class.phpmailer.php index 01c84e01..ca6de38e 100644 --- a/class.phpmailer.php +++ b/class.phpmailer.php @@ -2415,6 +2415,7 @@ class PHPMailer { /** * Evaluates the message and returns modifications for inline images and backgrounds + * Overwrites any existing values in $this->Body and $this->AltBody * @access public * @param string $message Text to be HTML modified * @param string $basedir baseline directory for path @@ -2444,18 +2445,22 @@ class PHPMailer { } $this->IsHTML(true); $this->Body = $message; - if (empty($this->AltBody)) { - $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s', '', $message))); - if (!empty($textMsg)) { - $this->AltBody = html_entity_decode($textMsg, ENT_QUOTES, $this->CharSet); - } - } + $this->AltBody = $this->html2text($message); if (empty($this->AltBody)) { $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . "\n\n"; } return $message; } + /** + * Convert an HTML string into a plain text version + * @param string $html The HTML text to convert + * @return string + */ + public function html2text($html) { + return html_entity_decode(trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s', '', $html))), ENT_QUOTES, $this->CharSet); + } + /** * Gets the MIME type of the embedded or inline image * @param string $ext File extension diff --git a/test/phpmailerTest.php b/test/phpmailerTest.php index d59459c7..992e5a80 100644 --- a/test/phpmailerTest.php +++ b/test/phpmailerTest.php @@ -716,19 +716,60 @@ class phpmailerTest extends PHPUnit_Framework_TestCase */ function test_Html() { - $this->Mail->IsHTML(true); $this->Mail->Subject .= ": HTML only"; - $this->Mail->Body = "This is a test message written in HTML.
" . - "Go to " . - "http://code.google.com/a/apache-extras.org/p/phpmailer/ for new versions of " . - "phpmailer.

Thank you!"; - + $this->Mail->Body = << + + HTML email test + + +

PHPMailer does HTML!

+

This is a test message written in HTML.
+ Go to http://code.google.com/a/apache-extras.org/p/phpmailer/ + for new versions of PHPMailer.

+

Thank you!

+ + +EOT; $this->BuildBody(); $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo); } + function test_MsgHTML() { + $message = << + + HTML email test + + +

PHPMailer does HTML!

+

This is a test message written in HTML.
+ Go to http://code.google.com/a/apache-extras.org/p/phpmailer/ + for new versions of PHPMailer.

+

Thank you!

+ + +EOT; + $this->Mail->MsgHTML($message); + $plainmessage = <<assertEquals($this->Mail->Body, $message, "Body not set by MsgHTML"); + $this->assertEquals($this->Mail->AltBody, $plainmessage, "AltBody not set by MsgHTML"); + //Make sure that changes to the original message are reflected when called again + $message = str_replace('PHPMailer', 'bananas', $message); + $plainmessage = str_replace('PHPMailer', 'bananas', $plainmessage); + $this->Mail->MsgHTML($message); + $this->assertEquals($this->Mail->Body, $message, "Body not updated by MsgHTML"); + $this->assertEquals($this->Mail->AltBody, $plainmessage, "AltBody not updated by MsgHTML"); + } /** * Simple HTML and attachment test */ @@ -990,10 +1031,10 @@ class phpmailerTest extends PHPUnit_Framework_TestCase $missing = array_diff(array_keys($definedStrings), array_keys($PHPMAILER_LANG)); $extra = array_diff(array_keys($PHPMAILER_LANG), array_keys($definedStrings)); if (!empty($missing)) { - $err .= "Missing translations in $lang: " . implode(', ', $missing) . "\n"; + $err .= "\nMissing translations in $lang: " . implode(', ', $missing); } if (!empty($extra)) { - $err .= "Extra translations in $lang: " . implode(', ', $extra) . "\n"; + $err .= "\nExtra translations in $lang: " . implode(', ', $extra); } } } @@ -1008,19 +1049,25 @@ class phpmailerTest extends PHPUnit_Framework_TestCase $this->Mail->CharSet = 'iso-8859-1'; $this->assertEquals( '=A1Hola!_Se=F1or!', - $this->Mail->EncodeQ('¡Hola! Señor!', 'text'), + $this->Mail->EncodeQ("\xa1Hola! Se\xf1or!", 'text'), 'Q Encoding (text) failed' ); $this->assertEquals( '=A1Hola!_Se=F1or!', - $this->Mail->EncodeQ('¡Hola! Señor!', 'comment'), + $this->Mail->EncodeQ("\xa1Hola! Se\xf1or!", 'comment'), 'Q Encoding (comment) failed' ); $this->assertEquals( '=A1Hola!_Se=F1or!', - $this->Mail->EncodeQ('¡Hola! Señor!', 'phrase'), + $this->Mail->EncodeQ("\xa1Hola! Se\xf1or!", 'phrase'), 'Q Encoding (phrase) failed' ); + $this->Mail->CharSet = 'UTF-8'; + $this->assertEquals( + '=C2=A1Hola!_Se=C3=B1or!', + $this->Mail->EncodeQ("\xc2\xa1Hola! Se\xc3\xb1or!", 'text'), + 'Q Encoding (text) failed' + ); } /**