Permalink
Browse files

Fix incorrectly generated boundaries for multipart messages.

Fix issue where multipart boundaries would be a simple string.
Fixes #2144
  • Loading branch information...
markstory committed Oct 23, 2011
1 parent fcc0aa4 commit 2bdb4fe70b63a9c85f793f58062ab466992d0491
@@ -400,13 +400,12 @@ 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[] = '';
}
$_method = '_' . $this->delivery;
$sent = $this->$_method();
@@ -492,7 +491,9 @@ function _render($content) {
$htmlContent = $View->element('email' . DS . 'html' . DS . $this->template, array('content' => $htmlContent), true);
$View->layoutPath = 'email' . DS . 'html';
$htmlContent = explode("\n", $this->htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
$msg = array_merge($msg, $htmlContent);
$msg[] = '';
$msg[] = '--alt-' . $this->__boundary . '--';
$msg[] = '';
@@ -609,8 +610,11 @@ function _createHeader() {
}
}
if (!empty($this->attachments)) {
if (!empty($this->attachments) || $this->sendAs === 'both') {
$this->_createBoundary();
}
if (!empty($this->attachments)) {
$headers['MIME-Version'] = '1.0';
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->__boundary . '"';
} elseif ($this->sendAs === 'text') {
@@ -559,6 +559,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'));
@@ -571,9 +572,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));
}
@@ -649,14 +651,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">
@@ -674,8 +686,11 @@ function testTemplates() {
HTMLBLOC;
$this->Controller->EmailTest->sendAs = 'html';
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . '</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 . '</pre>';
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
$result = ClassRegistry::getObject('view');

0 comments on commit 2bdb4fe

Please sign in to comment.