Skip to content

Commit

Permalink
Fix unique id / boundary problems when creating body before header
Browse files Browse the repository at this point in the history
Add tests for line length transfer encoding override
  • Loading branch information
Synchro committed Mar 20, 2015
1 parent 6e444ac commit a071e68
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
20 changes: 13 additions & 7 deletions class.phpmailer.php
Expand Up @@ -558,6 +558,13 @@ class PHPMailer
*/
protected $exceptions = false;

/**
* Unique ID used for message ID and boundaries.
* @type string
* @access protected
*/
protected $uniqueid = '';

/**
* Error severity: message only, continue processing.
*/
Expand Down Expand Up @@ -1694,12 +1701,6 @@ public function createHeader()
{
$result = '';

// Set the boundaries
$uniq_id = md5(uniqid(time()));
$this->boundary[1] = 'b1_' . $uniq_id;
$this->boundary[2] = 'b2_' . $uniq_id;
$this->boundary[3] = 'b3_' . $uniq_id;

if ($this->MessageDate == '') {
$this->MessageDate = self::rfcDate();
}
Expand Down Expand Up @@ -1751,7 +1752,7 @@ public function createHeader()
if ($this->MessageID != '') {
$this->lastMessageID = $this->MessageID;
} else {
$this->lastMessageID = sprintf('<%s@%s>', $uniq_id, $this->ServerHostname());
$this->lastMessageID = sprintf('<%s@%s>', $this->uniqueid, $this->ServerHostname());
}
$result .= $this->headerLine('Message-ID', $this->lastMessageID);
$result .= $this->headerLine('X-Priority', $this->Priority);
Expand Down Expand Up @@ -1861,6 +1862,11 @@ public function getSentMIMEMessage()
public function createBody()
{
$body = '';
//Create unique IDs and preset boundaries
$this->uniqueid = md5(uniqid(time()));
$this->boundary[1] = 'b1_' . $this->uniqueid;
$this->boundary[2] = 'b2_' . $this->uniqueid;
$this->boundary[3] = 'b3_' . $this->uniqueid;

if ($this->sign_key_file) {
$body .= $this->getMailMIME() . $this->LE;
Expand Down
13 changes: 12 additions & 1 deletion test/phpmailerTest.php
Expand Up @@ -1558,8 +1558,19 @@ public function testLineLength()
$this->assertTrue(PHPMailer::hasLineLongerThanMax($badlen), 'Long line not detected (only)');
$this->assertTrue(PHPMailer::hasLineLongerThanMax($oklen . $badlen), 'Long line not detected (first)');
$this->assertTrue(PHPMailer::hasLineLongerThanMax($badlen . $oklen), 'Long line not detected (last)');
$this->assertTrue(PHPMailer::hasLineLongerThanMax($oklen . $badlen . $oklen), 'Long line not detected (middle)');
$this->assertTrue(
PHPMailer::hasLineLongerThanMax($oklen . $badlen . $oklen),
'Long line not detected (middle)'
);
$this->assertFalse(PHPMailer::hasLineLongerThanMax($oklen), 'Long line false positive');
$this->Mail->isHTML(false);
$this->Mail->Subject .= ": Line length test";
$this->Mail->CharSet = 'UTF-8';
$this->Mail->Encoding = '8bit';
$this->Mail->Body = $oklen . $badlen . $oklen . $badlen;
$this->buildBody();
$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
$this->assertEquals('quoted-printable', $this->Mail->Encoding, 'Long line did not override transfer encoding');
}

/**
Expand Down

0 comments on commit a071e68

Please sign in to comment.