Implement an 'advanced' html to text converter for MsgHTML, using the html2text class we already bundle [via SourceForge](https://sourceforge.net/p/phpmailer/bugs/368/)

Some code cleanup in tests
This commit is contained in:
Synchro 2013-03-21 15:08:29 +01:00
parent ca65bf412c
commit 9a279e3a6f
4 changed files with 36 additions and 35 deletions

View File

@ -2422,14 +2422,16 @@ class PHPMailer {
}
/**
* Evaluates the message and returns modifications for inline images and backgrounds
* Creates a message from an HTML string, making modifications for inline images and backgrounds
* and creates a plain-text version by converting the HTML
* Overwrites any existing values in $this->Body and $this->AltBody
* @access public
* @param string $message Text to be HTML modified
* @param string $message HTML message string
* @param string $basedir baseline directory for path
* @param bool $advanced Whether to use the advanced HTML to text converter
* @return string $message
*/
public function MsgHTML($message, $basedir = '') {
public function MsgHTML($message, $basedir = '', $advanced = false) {
preg_match_all("/(src|background)=[\"'](.*)[\"']/Ui", $message, $images);
if(isset($images[2])) {
foreach($images[2] as $i => $url) {
@ -2453,19 +2455,25 @@ class PHPMailer {
}
$this->IsHTML(true);
$this->Body = $message;
$this->AltBody = $this->html2text($message);
$this->AltBody = $this->html2text($message, $advanced);
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) {
/**
* Convert an HTML string into a plain text version
* @param string $html The HTML text to convert
* @param bool $advanced Should this use the more complex html2text converter or just a simple one?
* @return string
*/
public function html2text($html, $advanced = false) {
if ($advanced) {
require_once 'extras/class.html2text.php';
$h = new html2text($html);
return $h->get_text();
}
return html_entity_decode(trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s', '', $html))), ENT_QUOTES, $this->CharSet);
}

View File

@ -2,7 +2,7 @@
/*************************************************************************
* *
* class.html2text.inc *
* class.html2text.php *
* *
*************************************************************************
* *
@ -232,8 +232,8 @@ class html2text
'--',
'-',
'*',
'£',
'EUR', // Euro sign. ?
'<EFBFBD>',
'EUR', // Euro sign. <EFBFBD> ?
'', // Unknown/unhandled entities
' ' // Runs of spaces, post-handling
);

View File

@ -58,7 +58,6 @@ class phpmailerTest extends PHPUnit_Framework_TestCase
*/
function setUp()
{
if (file_exists('./testbootstrap.php')) {
include './testbootstrap.php'; //Overrides go in here
}
@ -614,7 +613,6 @@ class phpmailerTest extends PHPUnit_Framework_TestCase
*/
function test_WordWrap()
{
$this->Mail->WordWrap = 40;
$my_body = 'Here is the main body of this message. It should ' .
'be quite a few lines. It should be wrapped at the ' .
@ -634,7 +632,6 @@ class phpmailerTest extends PHPUnit_Framework_TestCase
*/
function test_Low_Priority()
{
$this->Mail->Priority = 5;
$this->Mail->Body = 'Here is the main body. There should be ' .
'a reply to address in this message.';
@ -650,7 +647,6 @@ class phpmailerTest extends PHPUnit_Framework_TestCase
*/
function test_Multiple_Plain_FileAttachment()
{
$this->Mail->Body = 'Here is the text body';
$this->Mail->Subject .= ': Plain + Multiple FileAttachments';
@ -673,7 +669,6 @@ class phpmailerTest extends PHPUnit_Framework_TestCase
*/
function test_Plain_StringAttachment()
{
$this->Mail->Body = 'Here is the text body';
$this->Mail->Subject .= ': Plain + StringAttachment';
@ -692,7 +687,6 @@ class phpmailerTest extends PHPUnit_Framework_TestCase
*/
function test_Quoted_Printable()
{
$this->Mail->Body = 'Here is the main body';
$this->Mail->Subject .= ': Plain + Quoted-printable';
$this->Mail->Encoding = 'quoted-printable';
@ -718,7 +712,7 @@ class phpmailerTest extends PHPUnit_Framework_TestCase
$this->Mail->IsHTML(true);
$this->Mail->Subject .= ": HTML only";
$this->Mail->Body = <<<EOT
$this->Mail->Body = <<<'EOT'
<html>
<head>
<title>HTML email test</title>
@ -737,7 +731,7 @@ EOT;
}
function test_MsgHTML() {
$message = <<<EOT
$message = <<<'EOT'
<html>
<head>
<title>HTML email test</title>
@ -752,7 +746,7 @@ EOT;
</html>
EOT;
$this->Mail->MsgHTML($message);
$plainmessage = <<<EOT
$plainmessage = <<<'EOT'
PHPMailer does HTML!
This is a test message written in HTML.
Go to http://code.google.com/a/apache-extras.org/p/phpmailer/
@ -762,19 +756,25 @@ EOT;
$this->assertEquals($this->Mail->Body, $message, 'Body not set by MsgHTML');
$this->assertEquals($this->Mail->AltBody, $plainmessage, 'AltBody not set by MsgHTML');
//Again, using the advanced HTML to text converter
$this->Mail->AltBody = '';
$this->Mail->MsgHTML($message, '', true);
$this->assertNotEmpty($this->Mail->AltBody, 'Advanced 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
*/
function test_HTML_Attachment()
{
$this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
$this->Mail->Subject .= ': HTML + Attachment';
$this->Mail->IsHTML(true);
@ -793,7 +793,6 @@ EOT;
*/
function test_Embedded_Image()
{
$this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
'Here is an image!</a>';
$this->Mail->Subject .= ': Embedded Image';
@ -816,7 +815,6 @@ EOT;
//For code coverage
$this->Mail->AddEmbeddedImage('thisfiledoesntexist', 'xyz'); //Non-existent file
$this->Mail->AddEmbeddedImage(__FILE__, '123'); //Missing name
}
/**
@ -824,7 +822,6 @@ EOT;
*/
function test_Multi_Embedded_Image()
{
$this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
'Here is an image!</a>';
$this->Mail->Subject .= ': Embedded Image + Attachment';
@ -856,7 +853,6 @@ EOT;
*/
function test_AltBody()
{
$this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
$this->Mail->AltBody = 'Here is the text body of this message. ' .
'It should be quite a few lines. It should be wrapped at the ' .
@ -874,7 +870,6 @@ EOT;
*/
function test_AltBody_Attachment()
{
$this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
$this->Mail->AltBody = 'This is the text part of the email.';
$this->Mail->Subject .= ': AltBody + Attachment';

View File

@ -2,13 +2,11 @@
/*
* revised, updated and corrected 27/02/2013
* by matt.sturdy@gmail.com
*
*/
require_once("../class.phpmailer.php");
// below can be found at http://www.chuggnutt.com/html2text
// bundled in ./extras/
require_once('../extras/class.html2text.inc');
//This class from http://www.chuggnutt.com/html2text
require_once('../extras/class.html2text.php');
$CFG['smtp_debug'] = 1;
$CFG['smtp_server'] = 'mail.yourserver.com';
@ -42,7 +40,7 @@
// $example_code represents the "final code" that we're using, and will
// be shown to the user at the end.
$example_code = "\nrequire_once(\"../class.phpmailer.php\");";
$example_code .= "\nrequire_once(\"../extras/class.html2text.inc\");";
$example_code .= "\nrequire_once(\"../extras/class.html2text.php\");";
$example_code .= "\n\n\$results_messages = new array();";
class phpmailerAppException extends Exception {
@ -251,7 +249,7 @@
<link type="text/css" rel="stylesheet" href="styles/shThemeDefault.css"/>
<style>
body {
font-family: Arial, Helvetica, Sans-Serif;
font-family: Arial, Helvetica, sans-serif;
font-size: 1em;
padding: 1em;
}
@ -337,7 +335,7 @@
"smtp_authenticate": "<?php echo $smtp_authenticate; ?>",
"authenticate_username": "<?php echo $authenticate_username; ?>",
"authenticate_password": "<?php echo $authenticate_password; ?>"
}
};
var resetForm = document.createElement("form");
resetForm.setAttribute("method", "POST");