Skip to content
This repository
Browse code

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...
commit a6cf5f871c19fe43b50da2ecbdb2704e0b42aea5 1 parent f0af34b
Mark Story authored October 22, 2011
8  cake/libs/controller/components/email.php
@@ -335,7 +335,7 @@ function send($content = null, $template = null, $layout = null) {
335 335
 			$this->__attachFiles();
336 336
 		}
337 337
 
338  
-		if (!is_null($this->__boundary)) {
  338
+		if (!empty($this->attachments)) {
339 339
 			$this->__message[] = '';
340 340
 			$this->__message[] = '--' . $this->__boundary . '--';
341 341
 			$this->__message[] = '';
@@ -424,6 +424,7 @@ function __renderTemplate($content) {
424 424
 			$View->layoutPath = 'email' . DS . 'html';
425 425
 			$htmlContent = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
426 426
 			$msg = array_merge($msg, $htmlContent);
  427
+
427 428
 			$msg[] = '';
428 429
 			$msg[] = '--alt-' . $this->__boundary . '--';
429 430
 			$msg[] = '';
@@ -510,8 +511,11 @@ function __createHeader() {
510 511
 			}
511 512
 		}
512 513
 
513  
-		if (!empty($this->attachments)) {
  514
+		if (!empty($this->attachments) || $this->sendAs === 'both') {
514 515
 			$this->__createBoundary();
  516
+		}
  517
+
  518
+		if (!empty($this->attachments)) {
515 519
 			$this->__header[] = 'MIME-Version: 1.0';
516 520
 			$this->__header[] = 'Content-Type: multipart/mixed; boundary="' . $this->__boundary . '"';
517 521
 			$this->__header[] = 'This part of the E-mail should never be seen. If';
31  cake/tests/cases/libs/controller/components/email.test.php
@@ -393,6 +393,7 @@ function testSendFormats() {
393 393
 
394 394
 </pre>
395 395
 MSGBLOC;
  396
+
396 397
 		$this->Controller->EmailTest->sendAs = 'text';
397 398
 		$expect = str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $message);
398 399
 		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
@@ -405,9 +406,10 @@ function testSendFormats() {
405 406
 
406 407
 		// TODO: better test for format of message sent?
407 408
 		$this->Controller->EmailTest->sendAs = 'both';
408  
-		$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"', $message);
409  
-
410 409
 		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  410
+		$boundary = $this->Controller->EmailTest->getBoundary();
  411
+
  412
+		$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-' . $boundary . '"', $message);
411 413
 		$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
412 414
 	}
413 415
 /**
@@ -484,14 +486,24 @@ function testTemplates() {
484 486
 		$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
485 487
 
486 488
 		$this->Controller->EmailTest->sendAs = 'both';
487  
-		$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"', $header);
488  
-		$expect .= '--alt-' . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $text . "\n\n";
489  
-		$expect .= '--alt-' . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $html . "\n\n";
490  
-		$expect = '<pre>' . $expect . '--alt---' . "\n\n" . '</pre>';
491  
-
492 489
 		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  490
+		$boundary = $this->Controller->EmailTest->getBoundary();
  491
+
  492
+		$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-' . $boundary . '"', $header);
  493
+		$expect .= '--alt-' . $boundary . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $text . "\n\n";
  494
+		$expect .= '--alt-' . $boundary . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $html . "\n\n";
  495
+		$expect = '<pre>' . $expect . "--alt-$boundary--" . "\n\n" . '</pre>';
493 496
 		$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
494 497
 
  498
+		$this->Controller->EmailTest->reset();
  499
+		$this->Controller->EmailTest->to = 'postmaster@localhost';
  500
+		$this->Controller->EmailTest->from = 'noreply@example.com';
  501
+		$this->Controller->EmailTest->subject = 'Cake SMTP test';
  502
+		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
  503
+
  504
+		$this->Controller->EmailTest->delivery = 'debug';
  505
+		$this->Controller->EmailTest->messageId = false;
  506
+
495 507
 		$html = <<<HTMLBLOC
496 508
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
497 509
 
@@ -509,8 +521,11 @@ function testTemplates() {
509 521
 HTMLBLOC;
510 522
 
511 523
 		$this->Controller->EmailTest->sendAs = 'html';
512  
-		$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
513 524
 		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message', 'default', 'thin'));
  525
+
  526
+		$boundary = $this->Controller->EmailTest->getBoundary();
  527
+		$expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html;
  528
+		$expect = '<pre>' . $expect . "\n</pre>";
514 529
 		$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
515 530
 		
516 531
 		$result = ClassRegistry::getObject('view');

0 notes on commit a6cf5f8

Please sign in to comment.
Something went wrong with that request. Please try again.