Permalink
Browse files

Fix incorrectly generated boundaries for multipart messages.

Fix issue where multipart boundaries would be a simple string.
Fixes #2144

Conflicts:

	cake/libs/controller/components/email.php
	cake/tests/cases/libs/controller/components/email.test.php
  • Loading branch information...
1 parent f0af34b commit a6cf5f871c19fe43b50da2ecbdb2704e0b42aea5 @markstory markstory committed Oct 23, 2011
@@ -335,7 +335,7 @@ function send($content = null, $template = null, $layout = null) {
$this->__attachFiles();
}
- if (!is_null($this->__boundary)) {
+ if (!empty($this->attachments)) {
$this->__message[] = '';
$this->__message[] = '--' . $this->__boundary . '--';
$this->__message[] = '';
@@ -424,6 +424,7 @@ function __renderTemplate($content) {
$View->layoutPath = 'email' . DS . 'html';
$htmlContent = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
$msg = array_merge($msg, $htmlContent);
+
$msg[] = '';
$msg[] = '--alt-' . $this->__boundary . '--';
$msg[] = '';
@@ -510,8 +511,11 @@ function __createHeader() {
}
}
- if (!empty($this->attachments)) {
+ if (!empty($this->attachments) || $this->sendAs === 'both') {
$this->__createBoundary();
+ }
+
+ if (!empty($this->attachments)) {
$this->__header[] = 'MIME-Version: 1.0';
$this->__header[] = 'Content-Type: multipart/mixed; boundary="' . $this->__boundary . '"';
$this->__header[] = 'This part of the E-mail should never be seen. If';
@@ -393,6 +393,7 @@ function testSendFormats() {
</pre>
MSGBLOC;
+
$this->Controller->EmailTest->sendAs = 'text';
$expect = str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $message);
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
@@ -405,9 +406,10 @@ function testSendFormats() {
// 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'));
+ $boundary = $this->Controller->EmailTest->getBoundary();
+
+ $expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-' . $boundary . '"', $message);
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
}
/**
@@ -484,14 +486,24 @@ function testTemplates() {
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $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: 7bit' . "\n\n" . $text . "\n\n";
- $expect .= '--alt-' . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $html . "\n\n";
- $expect = '<pre>' . $expect . '--alt---' . "\n\n" . '</pre>';
-
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
+ $boundary = $this->Controller->EmailTest->getBoundary();
+
+ $expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-' . $boundary . '"', $header);
+ $expect .= '--alt-' . $boundary . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $text . "\n\n";
+ $expect .= '--alt-' . $boundary . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $html . "\n\n";
+ $expect = '<pre>' . $expect . "--alt-$boundary--" . "\n\n" . '</pre>';
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
+ $this->Controller->EmailTest->reset();
+ $this->Controller->EmailTest->to = 'postmaster@localhost';
+ $this->Controller->EmailTest->from = 'noreply@example.com';
+ $this->Controller->EmailTest->subject = 'Cake SMTP test';
+ $this->Controller->EmailTest->replyTo = 'noreply@example.com';
+
+ $this->Controller->EmailTest->delivery = 'debug';
+ $this->Controller->EmailTest->messageId = false;
+
$html = <<<HTMLBLOC
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
@@ -509,8 +521,11 @@ function testTemplates() {
HTMLBLOC;
$this->Controller->EmailTest->sendAs = 'html';
- $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message', 'default', 'thin'));
+
+ $boundary = $this->Controller->EmailTest->getBoundary();
+ $expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html;
+ $expect = '<pre>' . $expect . "\n</pre>";
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
$result = ClassRegistry::getObject('view');

0 comments on commit a6cf5f8

Please sign in to comment.