0
0
mirror of https://github.com/PHPMailer/PHPMailer.git synced 2024-09-20 01:52:15 +02:00

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
This commit is contained in:
Synchro 2013-02-28 14:21:00 +01:00
parent d50f51116f
commit c15920ece2
2 changed files with 69 additions and 17 deletions

View File

@ -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

View File

@ -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 <b>test message</b> written in HTML. </br>" .
"Go to <a href=\"http://code.google.com/a/apache-extras.org/p/phpmailer/\">" .
"http://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://code.google.com/a/apache-extras.org/p/phpmailer/">http://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://code.google.com/a/apache-extras.org/p/phpmailer/">http://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://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
*/
@ -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'
);
}
/**