Fix failing tests and missing boundary markers.

When sending html + text emails, there were duplicate multipart/alternative sections
and the trailing top level boundary was missing.
This commit is contained in:
Mark Story 2011-12-27 23:38:21 -05:00
parent f366a9ff83
commit 83b28c42cf
3 changed files with 28 additions and 18 deletions

View file

@ -662,14 +662,12 @@ class CakeEmail {
} }
$headers['MIME-Version'] = '1.0'; $headers['MIME-Version'] = '1.0';
if (!empty($this->_attachments)) { if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"'; $headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
} elseif ($this->_emailFormat === 'text') { } elseif ($this->_emailFormat === 'text') {
$headers['Content-Type'] = 'text/plain; charset=' . $this->charset; $headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
} elseif ($this->_emailFormat === 'html') { } elseif ($this->_emailFormat === 'html') {
$headers['Content-Type'] = 'text/html; charset=' . $this->charset; $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(); $headers['Content-Transfer-Encoding'] = $this->_getContentTransferEncoding();
@ -1377,6 +1375,8 @@ class CakeEmail {
if ($hasAttachments) { if ($hasAttachments) {
$attachments = $this->_attachFiles(); $attachments = $this->_attachFiles();
$msg = array_merge($msg, $attachments); $msg = array_merge($msg, $attachments);
}
if ($hasAttachments || $hasMultipleTypes) {
$msg[] = ''; $msg[] = '';
$msg[] = '--' . $boundary . '--'; $msg[] = '--' . $boundary . '--';
$msg[] = ''; $msg[] = '';

View file

@ -228,12 +228,6 @@ MSGBLOC;
$expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message); $expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message);
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message')); $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEquals(DebugCompTransport::$lastEmail, $this->__osFix($expect)); $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));
} }
/** /**
@ -307,13 +301,29 @@ HTMLBLOC;
$this->assertEquals(DebugCompTransport::$lastEmail, $this->__osFix($expect)); $this->assertEquals(DebugCompTransport::$lastEmail, $this->__osFix($expect));
$this->Controller->EmailTest->sendAs = 'both'; $this->Controller->EmailTest->sendAs = 'both';
$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"', $header); $expect = str_replace('{CONTENTTYPE}', 'multipart/mixed; boundary="{boundary}"', $header);
$expect .= '--alt-' . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 8bit' . "\n\n" . $text . "\n\n"; $expect .= "--{boundary}\n" .
$expect .= '--alt-' . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 8bit' . "\n\n" . $html . "\n\n"; 'Content-Type: multipart/alternative; boundary="alt-{boundary}"' . "\n\n" .
$expect = '<pre>' . $expect . '--alt---' . "\n\n" . '</pre>'; '--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->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 $html = <<<HTMLBLOC
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
@ -441,7 +451,7 @@ HTMLBLOC;
$this->Controller->EmailTest->delivery = 'DebugComp'; $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->Controller->EmailTest->sendAs = 'both';
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message')); $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
@ -740,8 +750,8 @@ HTMLBLOC;
$this->assertTrue($this->Controller->EmailTest->send($body)); $this->assertTrue($this->Controller->EmailTest->send($body));
$msg = DebugCompTransport::$lastEmail; $msg = DebugCompTransport::$lastEmail;
$this->assertNotRegExp('/text\/plain/', $msg); $this->assertRegExp('/text\/plain/', $msg);
$this->assertNotRegExp('/text\/html/', $msg); $this->assertRegExp('/text\/html/', $msg);
$this->assertRegExp('/multipart\/alternative/', $msg); $this->assertRegExp('/multipart\/alternative/', $msg);
} }

View file

@ -1080,7 +1080,7 @@ class CakeEmailTest extends CakeTestCase {
$boundary = $this->CakeEmail->getBoundary(); $boundary = $this->CakeEmail->getBoundary();
$this->assertFalse(empty($boundary)); $this->assertFalse(empty($boundary));
$this->assertContains('--' . $boundary, $message); $this->assertContains('--' . $boundary, $message);
$this->assertNotContains('--' . $boundary . '--', $message); $this->assertContains('--' . $boundary . '--', $message);
$this->assertContains('--alt-' . $boundary, $message); $this->assertContains('--alt-' . $boundary, $message);
$this->assertContains('--alt-' . $boundary . '--', $message); $this->assertContains('--alt-' . $boundary . '--', $message);