diff --git a/lib/Cake/Network/CakeEmail.php b/lib/Cake/Network/CakeEmail.php index d1c78c2cbf5..9f5a73a3df5 100644 --- a/lib/Cake/Network/CakeEmail.php +++ b/lib/Cake/Network/CakeEmail.php @@ -601,7 +601,6 @@ public function getHeaders($include = array()) { } if (!empty($this->_attachments)) { - $this->_createBoundary(); $headers['MIME-Version'] = '1.0'; $headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"'; $headers[] = 'This part of the E-mail should never be seen. If'; @@ -892,6 +891,8 @@ public function send($content = null) { } } + $this->_createBoundary(); + $message = $this->_wrap($content); if (empty($this->_template)) { $message = $this->_formatMessage($message); @@ -905,7 +906,7 @@ public function send($content = null) { $this->_attachFiles(); } - if (!is_null($this->_boundary)) { + if (!empty($this->_attachments)) { $this->_message[] = ''; $this->_message[] = '--' . $this->_boundary . '--'; $this->_message[] = ''; @@ -1147,8 +1148,10 @@ protected function _wrap($message) { * * @return void */ - protected function _createboundary() { - $this->_boundary = md5(uniqid(time())); + protected function _createBoundary() { + if (!empty($this->_attachments) || $this->_emailFormat === 'both') { + $this->_boundary = md5(uniqid(time())); + } } /** diff --git a/lib/Cake/Network/Email/SmtpTransport.php b/lib/Cake/Network/Email/SmtpTransport.php index aba0bd9f2fe..6713d68bbc2 100644 --- a/lib/Cake/Network/Email/SmtpTransport.php +++ b/lib/Cake/Network/Email/SmtpTransport.php @@ -161,6 +161,7 @@ protected function _sendData() { $headers = $this->_cakeEmail->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), true)); $headers = $this->_headersToString($headers); $message = implode("\r\n", $this->_cakeEmail->message()); + pr($headers . "\r\n\r\n" . $message . "\r\n\r\n\r\n."); $this->_smtpSend($headers . "\r\n\r\n" . $message . "\r\n\r\n\r\n."); } diff --git a/lib/Cake/tests/Case/Network/CakeEmailTest.php b/lib/Cake/tests/Case/Network/CakeEmailTest.php index bdef865bcaf..382b377e93b 100644 --- a/lib/Cake/tests/Case/Network/CakeEmailTest.php +++ b/lib/Cake/tests/Case/Network/CakeEmailTest.php @@ -46,6 +46,15 @@ public function wrap($text) { return parent::_wrap($text); } +/** + * Get the boundary attribute + * + * @return string + */ + public function getBoundary() { + return $this->_boundary; + } + } /** @@ -580,6 +589,45 @@ public function testSendRenderWithVars() { $this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Here is your value: 12345')); } +/** + * testSendMultipleMIME method + * + * @return void + */ + public function testSendMultipleMIME() { + $this->CakeEmail->reset(); + $this->CakeEmail->transport('debug'); + DebugTransport::$includeAddresses = true; + + $this->CakeEmail->from('cake@cakephp.org'); + $this->CakeEmail->to(array('you@cakephp.org' => 'You')); + $this->CakeEmail->subject('My title'); + $this->CakeEmail->template('custom', 'default'); + $this->CakeEmail->config(array()); + $this->CakeEmail->viewVars(array('value' => 12345)); + $this->CakeEmail->emailFormat('both'); + $result = $this->CakeEmail->send(); + + $message = $this->CakeEmail->message(); + $boundary = $this->CakeEmail->getBoundary(); + $this->assertFalse(empty($boundary)); + $this->assertFalse(in_array('--' . $boundary, $message)); + $this->assertFalse(in_array('--' . $boundary . '--', $message)); + $this->assertTrue(in_array('--alt-' . $boundary, $message)); + $this->assertTrue(in_array('--alt-' . $boundary . '--', $message)); + + $this->CakeEmail->attachments(array('fake.php' => __FILE__)); + $this->CakeEmail->send(); + + $message = $this->CakeEmail->message(); + $boundary = $this->CakeEmail->getBoundary(); + $this->assertFalse(empty($boundary)); + $this->assertTrue(in_array('--' . $boundary, $message)); + $this->assertTrue(in_array('--' . $boundary . '--', $message)); + $this->assertTrue(in_array('--alt-' . $boundary, $message)); + $this->assertTrue(in_array('--alt-' . $boundary . '--', $message)); + } + /** * testFastSend method *