Skip to content

Commit

Permalink
Make MsgHTML() always overwrite AltBody, fixes #28
Browse files Browse the repository at this point in the history
Break out html to text conversion to a method so it can be overridden easily and use it internally, fixes #29
  • Loading branch information
Synchro committed Feb 28, 2013
1 parent d50f511 commit c15920e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
17 changes: 11 additions & 6 deletions class.phpmailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,7 @@ public function AddCustomHeader($name, $value=null) {

/**
* 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
Expand Down Expand Up @@ -2444,18 +2445,22 @@ public function MsgHTML($message, $basedir = '') {
}
$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
Expand Down
69 changes: 58 additions & 11 deletions test/phpmailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,19 +716,60 @@ function test_Quoted_Printable()
*/
function test_Html()
{

$this->Mail->IsHTML(true);
$this->Mail->Subject .= ": HTML only";

$this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .
"Go to <a href=\"http:https://code.google.com/a/apache-extras.org/p/phpmailer/\">" .
"http:https://code.google.com/a/apache-extras.org/p/phpmailer/</a> for new versions of " .
"phpmailer. <p/> Thank you!";

$this->Mail->Body = <<<EOT
<html>
<head>
<title>HTML email test</title>
</head>
<body>
<h1>PHPMailer does HTML!</h1>
<p>This is a <strong>test message</strong> written in HTML.<br>
Go to <a href="http:https://code.google.com/a/apache-extras.org/p/phpmailer/">http:https://code.google.com/a/apache-extras.org/p/phpmailer/</a>
for new versions of PHPMailer.</p>
<p>Thank you!</p>
</body>
</html>
EOT;
$this->BuildBody();
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
}

function test_MsgHTML() {
$message = <<<EOT
<html>
<head>
<title>HTML email test</title>
</head>
<body>
<h1>PHPMailer does HTML!</h1>
<p>This is a <strong>test message</strong> written in HTML.<br>
Go to <a href="http:https://code.google.com/a/apache-extras.org/p/phpmailer/">http:https://code.google.com/a/apache-extras.org/p/phpmailer/</a>
for new versions of PHPMailer.</p>
<p>Thank you!</p>
</body>
</html>
EOT;
$this->Mail->MsgHTML($message);
$plainmessage = <<<EOT
PHPMailer does HTML!
This is a test message written in HTML.
Go to http:https://code.google.com/a/apache-extras.org/p/phpmailer/
for new versions of PHPMailer.
Thank you!
EOT;

$this->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
*/
Expand Down Expand Up @@ -990,10 +1031,10 @@ function test_Translations()
$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);
}
}
}
Expand All @@ -1008,19 +1049,25 @@ function test_Encodings()
$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'
);
}

/**
Expand Down

0 comments on commit c15920e

Please sign in to comment.