diff --git a/cake/libs/controller/components/email.php b/cake/libs/controller/components/email.php index 6868c21cd..1203ac20c 100644 --- a/cake/libs/controller/components/email.php +++ b/cake/libs/controller/components/email.php @@ -504,12 +504,16 @@ class EmailComponent extends Object{ */ function __formatMessage($message) { if (!empty($this->attachments)) { - $prefix = array( - '--' . $this->__boundary, - 'Content-Type: text/plain; charset=' . $this->charset, - 'Content-Transfer-Encoding: 7bit', - '' - ); + $prefix[] = '--' . $this->__boundary; + if ($this->sendAs === 'text') { + $prefix[] = 'Content-Type: text/plain; charset=' . $this->charset; + } elseif ($this->sendAs === 'html') { + $prefix[] = 'Content-Type: text/html; charset=' . $this->charset; + } elseif ($this->sendAs === 'both') { + $prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"'; + } + $prefix[] = 'Content-Transfer-Encoding: 7bit'; + $prefix[] = ''; $message = array_merge($prefix, $message); } return $message; diff --git a/cake/tests/cases/libs/controller/components/email.test.php b/cake/tests/cases/libs/controller/components/email.test.php index f8090cf5f..3527e7616 100644 --- a/cake/tests/cases/libs/controller/components/email.test.php +++ b/cake/tests/cases/libs/controller/components/email.test.php @@ -556,6 +556,42 @@ TEXTBLOC; preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches); $this->assertEqual(trim($matches[1]), $subject); } +/** + * undocumented function + * + * @return void + * @access public + */ + function testSendAsIsNotIgnoredIfAttachmentsPresent() { + $this->Controller->EmailTest->reset(); + $this->Controller->EmailTest->to = 'postmaster@localhost'; + $this->Controller->EmailTest->from = 'noreply@example.com'; + $this->Controller->EmailTest->subject = 'Attachment Test'; + $this->Controller->EmailTest->replyTo = 'noreply@example.com'; + $this->Controller->EmailTest->template = null; + $this->Controller->EmailTest->delivery = 'debug'; + $this->Controller->EmailTest->attachments = array(__FILE__); + $body = '
This is the body of the message
'; + + $this->Controller->EmailTest->sendAs = 'html'; + $this->assertTrue($this->Controller->EmailTest->send($body)); + $msg = $this->Controller->Session->read('Message.email.message'); + $this->assertNoPattern('/text\/plain/', $msg); + $this->assertPattern('/text\/html/', $msg); + + $this->Controller->EmailTest->sendAs = 'text'; + $this->assertTrue($this->Controller->EmailTest->send($body)); + $msg = $this->Controller->Session->read('Message.email.message'); + $this->assertPattern('/text\/plain/', $msg); + $this->assertNoPattern('/text\/html/', $msg); + + $this->Controller->EmailTest->sendAs = 'both'; + $this->assertTrue($this->Controller->EmailTest->send($body)); + $msg = $this->Controller->Session->read('Message.email.message'); + $this->assertNoPattern('/text\/plain/', $msg); + $this->assertNoPattern('/text\/html/', $msg); + $this->assertPattern('/multipart\/alternative/', $msg); + } /** * testReset method *