Skip to content

Commit

Permalink
Convert line breaks to CRLF in MsgHTML, closes #52
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Synchro committed May 1, 2013
1 parent 93ccc05 commit 019499e
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 162 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
docs/phpdoc/
test/message.txt
test/testbootstrap.php
.idea
23 changes: 19 additions & 4 deletions class.phpmailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ protected function AttachAll($disposition_type, $boundary) {
$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
Expand Down Expand Up @@ -2497,12 +2497,13 @@ public function MsgHTML($message, $basedir = '', $advanced = false) {
}
}
$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;
}

/**
Expand Down Expand Up @@ -2723,6 +2724,20 @@ public function SecureHeader($str) {
}

/**
* 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
Expand Down
44 changes: 0 additions & 44 deletions docs/use_gmail.txt

This file was deleted.

2 changes: 1 addition & 1 deletion examples/contents.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:https://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>
Expand Down
13 changes: 0 additions & 13 deletions test/contents.html

This file was deleted.

73 changes: 36 additions & 37 deletions test/phpmailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ function test_Multiple_Plain_FileAttachment()
$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;
}
Expand Down Expand Up @@ -730,46 +730,30 @@ function test_Html()
$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="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/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
*/
Expand Down Expand Up @@ -799,9 +783,9 @@ function test_Embedded_Image()
$this->Mail->IsHTML(true);

if (!$this->Mail->AddEmbeddedImage(
'test.png',
'../examples/images/phpmailer.png',
'my-attach',
'test.png',
'phpmailer.png',
'base64',
'image/png'
)
Expand All @@ -828,9 +812,9 @@ function test_Multi_Embedded_Image()
$this->Mail->IsHTML(true);

if (!$this->Mail->AddEmbeddedImage(
'test.png',
'../examples/images/phpmailer.png',
'my-attach',
'test.png',
'phpmailer.png',
'base64',
'image/png'
)
Expand Down Expand Up @@ -1136,6 +1120,21 @@ function test_Signing()
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
*/
Expand Down
Binary file removed test/test.png
Binary file not shown.
23 changes: 8 additions & 15 deletions test/test_callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
48 changes: 0 additions & 48 deletions test/testemail.php

This file was deleted.

0 comments on commit 019499e

Please sign in to comment.