From c5cbb603a06769e772f1290cc378070722f54b57 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Thu, 3 Mar 2011 23:05:30 -0300 Subject: [PATCH] Added the methods to format message. --- lib/Cake/Network/CakeEmail.php | 50 ++++++++++++++++++- lib/Cake/tests/Case/Network/CakeEmailTest.php | 16 ++++-- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Network/CakeEmail.php b/lib/Cake/Network/CakeEmail.php index a7d8e88aba6..cdd3d2b96e4 100644 --- a/lib/Cake/Network/CakeEmail.php +++ b/lib/Cake/Network/CakeEmail.php @@ -606,6 +606,22 @@ public function getHeaders($include = array()) { $headers['Subject'] = $this->_subject; } + 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'; + $headers[] = 'you are reading this, consider upgrading your e-mail'; + $headers[] = 'client to a MIME-compatible client.'; + } elseif ($this->_emailFormat === 'text') { + $headers['Content-Type'] = 'text/plain; charset=' . $this->charset; + } elseif ($this->_emailFormat === 'html') { + $headers['Content-Type'] = 'text/html; charset=' . $this->charset; + } elseif ($this->_emailFormat === 'both') { + $headers['Content-Type'] = 'multipart/alternative; boundary="alt-' . $this->_boundary . '"'; + } + $headers['Content-Transfer-Encoding'] = '7bit'; + return $headers; } @@ -764,7 +780,7 @@ public function send($content = null) { $message = $this->_wrap($content); if (empty($this->template)) { - //$message = $this->_formatMessage($message); + $message = $this->_formatMessage($message); } else { //$message = $this->_render($message); } @@ -933,4 +949,36 @@ function _wrap($message) { return $formatted; } +/** + * Create unique boundary identifier + * + * @return void + */ + function _createboundary() { + $this->_boundary = md5(uniqid(time())); + } + +/** + * Format the message by seeing if it has attachments. + * + * @param array $message Message to format + * @return array + */ + function _formatMessage($message) { + if (!empty($this->_attachments)) { + $prefix = array('--' . $this->_boundary); + if ($this->_emailFormat === 'text') { + $prefix[] = 'Content-Type: text/plain; charset=' . $this->charset; + } elseif ($this->_emailFormat === 'html') { + $prefix[] = 'Content-Type: text/html; charset=' . $this->charset; + } elseif ($this->_emailFormat === '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/lib/Cake/tests/Case/Network/CakeEmailTest.php b/lib/Cake/tests/Case/Network/CakeEmailTest.php index 14a6dafbd41..53b887975ca 100644 --- a/lib/Cake/tests/Case/Network/CakeEmailTest.php +++ b/lib/Cake/tests/Case/Network/CakeEmailTest.php @@ -232,7 +232,9 @@ public function testHeaders() { $expected = array( 'X-Something' => 'nice', 'X-Mailer' => 'CakePHP Email Component', - 'Date' => date(DATE_RFC2822) + 'Date' => date(DATE_RFC2822), + 'Content-Type' => 'text/plain; charset=UTF-8', + 'Content-Transfer-Encoding' => '7bit' ); $this->assertIdentical($this->CakeEmail->getHeaders(), $expected); @@ -241,7 +243,9 @@ public function testHeaders() { 'X-Something' => 'very nice', 'X-Other' => 'cool', 'X-Mailer' => 'CakePHP Email Component', - 'Date' => date(DATE_RFC2822) + 'Date' => date(DATE_RFC2822), + 'Content-Type' => 'text/plain; charset=UTF-8', + 'Content-Transfer-Encoding' => '7bit' ); $this->assertIdentical($this->CakeEmail->getHeaders(), $expected); @@ -253,7 +257,9 @@ public function testHeaders() { 'X-Something' => 'very nice', 'X-Other' => 'cool', 'X-Mailer' => 'CakePHP Email Component', - 'Date' => date(DATE_RFC2822) + 'Date' => date(DATE_RFC2822), + 'Content-Type' => 'text/plain; charset=UTF-8', + 'Content-Transfer-Encoding' => '7bit' ); $this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true)), $expected); @@ -268,7 +274,9 @@ public function testHeaders() { 'X-Something' => 'very nice', 'X-Other' => 'cool', 'X-Mailer' => 'CakePHP Email Component', - 'Date' => date(DATE_RFC2822) + 'Date' => date(DATE_RFC2822), + 'Content-Type' => 'text/plain; charset=UTF-8', + 'Content-Transfer-Encoding' => '7bit' ); $this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected); }