Convert line breaks to CRLF in MsgHTML, closes #52

Fix double suffix on image cids, see #50
Remove unneeded test files, re-use example content for tests
Remove reference to phpmailer-lite
Ignore .idea folder
This commit is contained in:
Marcus Bointon 2013-05-01 10:20:10 +02:00
parent 93ccc051a8
commit 019499efc3
9 changed files with 65 additions and 162 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
docs/phpdoc/
test/message.txt
test/testbootstrap.php
.idea

View File

@ -1845,7 +1845,7 @@ class PHPMailer {
$mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
if($disposition == 'inline') {
$mime[] = sprintf("Content-ID: <%s@phpmailer.0>%s", $cid, $this->LE); //RFC2392 S 2
$mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
}
//If a filename contains any of these chars, it should be quoted, but not otherwise: RFC2183 & RFC2045 5.1
@ -2497,12 +2497,13 @@ class PHPMailer {
}
}
$this->IsHTML(true);
$this->Body = $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 all message body line breaks to CRLF, makes quoted-printable encoding work much better
$this->Body = $this->NormalizeBreaks($message);
$this->AltBody = $this->NormalizeBreaks($this->html2text($message, $advanced));
return $this->Body;
}
/**
@ -2723,6 +2724,20 @@ class PHPMailer {
}
/**
* Normalize UNIX LF, Mac CR and Windows CRLF line breaks into a single line break format
* Defaults to CRLF (for message bodies) and preserves consecutive breaks
* @param string $text
* @param string $breaktype What kind of line break to use, defaults to CRLF
* @return string
* @access public
* @static
*/
public static function NormalizeBreaks($text, $breaktype = "\r\n") {
return preg_replace('/(\r\n|\r|\n)/ms', $breaktype, $text);
}
/**
* Set the private key file and password to sign the message.
*
* @access public

View File

@ -1,44 +0,0 @@
<?php
// example on using PHPMailer with GMAIL
include("class.phpmailer.php");
include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port
$mail->Username = "yourname@gmail.com"; // GMAIL username
$mail->Password = "password"; // GMAIL password
$mail->From = "replyto@yourdomain.com";
$mail->FromName = "Webmaster";
$mail->Subject = "This is the subject";
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddReplyTo("replyto@yourdomain.com","Webmaster");
$mail->AddAttachment("/path/to/file.zip"); // attachment
$mail->AddAttachment("/path/to/image.jpg", "new.jpg"); // attachment
$mail->AddAddress("username@domain.com","First Last");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>

View File

@ -1,7 +1,7 @@
<!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=utf-8">
<title>PHPMailer Test</title>
</head>
<body>

View File

@ -1,13 +0,0 @@
<!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>
<p>This example uses <strong>HTML</strong>.</p>
</div>
</body>
</html>

View File

@ -650,7 +650,7 @@ class phpmailerTest extends PHPUnit_Framework_TestCase
$this->Mail->Body = 'Here is the text body';
$this->Mail->Subject .= ': Plain + Multiple FileAttachments';
if (!$this->Mail->AddAttachment('test.png')) {
if (!$this->Mail->AddAttachment('../examples/images/phpmailer.png')) {
$this->assertTrue(false, $this->Mail->ErrorInfo);
return;
}
@ -730,46 +730,30 @@ EOT;
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
}
/**
* Test simple message builder and html2text converters
*/
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="https://github.com/PHPMailer/PHPMailer/">https://github.com/PHPMailer/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 https://github.com/PHPMailer/PHPMailer/
for new versions of PHPMailer.
Thank you!
EOT;
$message = file_get_contents('../examples/contents.html');
$this->Mail->CharSet = 'utf-8';
$this->Mail->Body = '';
$this->Mail->AltBody = '';
$this->Mail->MsgHTML($message, '..');
$this->Mail->Subject .= ': MsgHTML';
$this->assertEquals($this->Mail->Body, $message, 'Body not set by MsgHTML');
$this->assertEquals($this->Mail->AltBody, $plainmessage, 'AltBody not set by MsgHTML');
$this->assertNotEmpty($this->Mail->Body, 'Body not set by MsgHTML');
$this->assertNotEmpty($this->Mail->AltBody, 'AltBody not set by MsgHTML');
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
//Again, using the advanced HTML to text converter
$this->Mail->AltBody = '';
$this->Mail->MsgHTML($message, '', true);
$this->Mail->MsgHTML($message, '..', true);
$this->Mail->Subject .= ' + html2text advanced';
$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');
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
}
/**
* Simple HTML and attachment test
*/
@ -799,9 +783,9 @@ EOT;
$this->Mail->IsHTML(true);
if (!$this->Mail->AddEmbeddedImage(
'test.png',
'../examples/images/phpmailer.png',
'my-attach',
'test.png',
'phpmailer.png',
'base64',
'image/png'
)
@ -828,9 +812,9 @@ EOT;
$this->Mail->IsHTML(true);
if (!$this->Mail->AddEmbeddedImage(
'test.png',
'../examples/images/phpmailer.png',
'my-attach',
'test.png',
'phpmailer.png',
'base64',
'image/png'
)
@ -1136,6 +1120,21 @@ EOT;
unlink($keyfile);
}
/**
* Test line break reformatting
*/
function test_LineBreaks()
{
$unixsrc = "Hello\nWorld\nAgain\n";
$macsrc = "Hello\rWorld\rAgain\r";
$windowssrc = "Hello\r\nWorld\r\nAgain\r\n";
$mixedsrc = "Hello\nWorld\rAgain\r\n";
$target = "Hello\r\nWorld\r\nAgain\r\n";
$this->assertEquals($target, PHPMailer::NormalizeBreaks($unixsrc), 'UNIX break reformatting failed');
$this->assertEquals($target, PHPMailer::NormalizeBreaks($macsrc), 'Mac break reformatting failed');
$this->assertEquals($target, PHPMailer::NormalizeBreaks($windowssrc), 'Windows break reformatting failed');
$this->assertEquals($target, PHPMailer::NormalizeBreaks($mixedsrc), 'Mixed break reformatting failed');
}
/**
* Miscellaneous calls to improve test coverage and some small tests
*/

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -30,25 +30,18 @@ function callbackAction ($result, $to, $cc, $bcc, $subject, $body) {
return true;
}
$testLite = false;
if ($testLite) {
require_once '../class.phpmailer-lite.php';
$mail = new PHPMailerLite();
} else {
require_once '../class.phpmailer.php';
$mail = new PHPMailer();
}
require_once '../class.phpmailer.php';
$mail = new PHPMailer();
try {
$mail->IsMail(); // telling the class to use SMTP
$mail->SetFrom('you@yourdomain.com', 'Your Name');
$mail->AddAddress('another@yourdomain.com', 'John Doe');
$mail->IsMail();
$mail->SetFrom('you@example.com', 'Your Name');
$mail->AddAddress('another@example.com', 'John Doe');
$mail->Subject = 'PHPMailer Lite Test Subject via Mail()';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.png'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->MsgHTML(file_get_contents('../examples/contents.html'));
$mail->AddAttachment('../examples/images/phpmailer.png'); // attachment
$mail->AddAttachment('../examples/images/phpmailer_mini.gif'); // attachment
$mail->action_function = 'callbackAction';
$mail->Send();
echo "Message Sent OK</p>\n";

View File

@ -1,48 +0,0 @@
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/
require '../class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = file_get_contents('contents.html');
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->Username = "name@domain.com"; // SMTP server username
$mail->Password = "password"; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("name@domain.com","First Last");
$mail->From = "name@domain.com";
$mail->FromName = "First Last";
$to = "someone@example...com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>