Skip to content

Commit

Permalink
Fix failing tests and missing boundary markers.
Browse files Browse the repository at this point in the history
When sending html + text emails, there were duplicate multipart/alternative sections
and the trailing top level boundary was missing.
  • Loading branch information
markstory committed Dec 28, 2011
1 parent f366a9f commit 83b28c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/Cake/Network/Email/CakeEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,14 +662,12 @@ public function getHeaders($include = array()) {
}

$headers['MIME-Version'] = '1.0';
if (!empty($this->_attachments)) {
if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
} elseif ($this->_emailFormat === 'text') {
$headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
} elseif ($this->_emailFormat === 'html') {
$headers['Content-Type'] = 'text/html; charset=' . $this->charset;
} elseif ($this->_emailFormat === 'both') {
$headers['Content-Type'] = 'multipart/alternative; boundary="alt-' . $this->_boundary . '"';
}
$headers['Content-Transfer-Encoding'] = $this->_getContentTransferEncoding();

Expand Down Expand Up @@ -1377,6 +1375,8 @@ protected function _render($content) {
if ($hasAttachments) {
$attachments = $this->_attachFiles();
$msg = array_merge($msg, $attachments);
}
if ($hasAttachments || $hasMultipleTypes) {
$msg[] = '';
$msg[] = '--' . $boundary . '--';
$msg[] = '';
Expand Down
38 changes: 24 additions & 14 deletions lib/Cake/Test/Case/Controller/Component/EmailComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ public function testSendFormats() {
$expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message);
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEquals(DebugCompTransport::$lastEmail, $this->__osFix($expect));

// TODO: better test for format of message sent?
$this->Controller->EmailTest->sendAs = 'both';
$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"', $message);
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEquals(preg_replace('/alt-[a-z0-9]{32}/i', 'alt-', DebugCompTransport::$lastEmail), $this->__osFix($expect));
}

/**
Expand Down Expand Up @@ -307,13 +301,29 @@ public function testTemplates() {
$this->assertEquals(DebugCompTransport::$lastEmail, $this->__osFix($expect));

$this->Controller->EmailTest->sendAs = 'both';
$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"', $header);
$expect .= '--alt-' . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 8bit' . "\n\n" . $text . "\n\n";
$expect .= '--alt-' . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 8bit' . "\n\n" . $html . "\n\n";
$expect = '<pre>' . $expect . '--alt---' . "\n\n" . '</pre>';
$expect = str_replace('{CONTENTTYPE}', 'multipart/mixed; boundary="{boundary}"', $header);
$expect .= "--{boundary}\n" .
'Content-Type: multipart/alternative; boundary="alt-{boundary}"' . "\n\n" .
'--alt-{boundary}' . "\n" .
'Content-Type: text/plain; charset=UTF-8' . "\n" .
'Content-Transfer-Encoding: 8bit' . "\n\n" .
$text .
"\n\n" .
'--alt-{boundary}' . "\n" .
'Content-Type: text/html; charset=UTF-8' . "\n" .
'Content-Transfer-Encoding: 8bit' . "\n\n" .
$html .
"\n\n" .
'--alt-{boundary}--' . "\n\n\n" .
'--{boundary}--' . "\n";

$expect = '<pre>' . $expect . '</pre>';

$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEquals(preg_replace('/alt-[a-z0-9]{32}/i', 'alt-', DebugCompTransport::$lastEmail), $this->__osFix($expect));
$this->assertEquals(
$this->__osFix($expect),
preg_replace('/[a-z0-9]{32}/i', '{boundary}', DebugCompTransport::$lastEmail)
);

$html = <<<HTMLBLOC
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
Expand Down Expand Up @@ -441,7 +451,7 @@ public function testMessageRetrievalWithoutTemplate() {

$this->Controller->EmailTest->delivery = 'DebugComp';

$text = $html = 'This is the body of the message';
$text = $html = "This is the body of the message\n";

$this->Controller->EmailTest->sendAs = 'both';
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
Expand Down Expand Up @@ -740,8 +750,8 @@ public function testSendAsIsNotIgnoredIfAttachmentsPresent() {
$this->assertTrue($this->Controller->EmailTest->send($body));
$msg = DebugCompTransport::$lastEmail;

$this->assertNotRegExp('/text\/plain/', $msg);
$this->assertNotRegExp('/text\/html/', $msg);
$this->assertRegExp('/text\/plain/', $msg);
$this->assertRegExp('/text\/html/', $msg);
$this->assertRegExp('/multipart\/alternative/', $msg);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ public function testSendMultipleMIME() {
$boundary = $this->CakeEmail->getBoundary();
$this->assertFalse(empty($boundary));
$this->assertContains('--' . $boundary, $message);
$this->assertNotContains('--' . $boundary . '--', $message);
$this->assertContains('--' . $boundary . '--', $message);
$this->assertContains('--alt-' . $boundary, $message);
$this->assertContains('--alt-' . $boundary . '--', $message);

Expand Down

0 comments on commit 83b28c4

Please sign in to comment.