From 6930ba62073fb548ea2b943c0eaed77a87cb4283 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Tue, 1 Mar 2011 15:16:50 -0300 Subject: [PATCH] Changes to in manipulation of header. --- lib/Cake/Network/CakeEmail.php | 88 ++++++++++++++++--- lib/Cake/tests/Case/Network/CakeEmailTest.php | 30 +++++++ 2 files changed, 105 insertions(+), 13 deletions(-) diff --git a/lib/Cake/Network/CakeEmail.php b/lib/Cake/Network/CakeEmail.php index 2e9a1e0fa..cef656978 100644 --- a/lib/Cake/Network/CakeEmail.php +++ b/lib/Cake/Network/CakeEmail.php @@ -501,25 +501,87 @@ class CakeEmail { /** * Get list of headers * - * @param boolean $includeToAndCc - * @param boolean $includeBcc - * @param boolean $includeSubject + * ### Includes: + * + * - `from` + * - `replyTo` + * - `readReceipt` + * - `returnPath` + * - `to` + * - `cc` + * - `bcc` + * - `subject` + * + * @param array $include * @return array */ - public function getHeaders($includeToAndCc = false, $includeBcc = false, $includeSubject = false) { - if (!isset($this->_headers['X-Mailer'])) { - $this->_headers['X-Mailer'] = Configure::read('Email.XMailer'); - if (empty($this->_headers['X-Mailer'])) { - $this->_headers['X-Mailer'] = self::EMAIL_CLIENT; + public function getHeaders($include = array()) { + $defaults = array( + 'from' => false, + 'replyTo' => false, + 'readReceipt' => false, + 'returnPath' => false, + 'to' => false, + 'cc' => false, + 'bcc' => false, + 'subject' => false + ); + $include += $defaults; + + $headers = $this->_headers; + if (!isset($headers['X-Mailer'])) { + $headers['X-Mailer'] = Configure::read('Email.XMailer'); + if (empty($headers['X-Mailer'])) { + $headers['X-Mailer'] = self::EMAIL_CLIENT; } } - if (!isset($this->_headers['Date'])) { - $this->_headers['Date'] = date(DATE_RFC2822); + if (!isset($headers['Date'])) { + $headers['Date'] = date(DATE_RFC2822); } - if ($includeSubject) { - $this->_headers['Subject'] = $this->_subject; + + $relation = array( + 'from' => 'From', + 'replyTo' => 'Reply-To', + 'readReceipt' => 'Disposition-Notification-To', + 'returnPath' => 'Return-Path' + ); + foreach ($relation as $var => $header) { + if ($include[$var]) { + $var = '_' . $var; + $headers[$header] = current($this->_formatAddress($this->{$var})); + } } - return $this->_headers; + + foreach (array('to', 'cc', 'bcc') as $var) { + if ($include[$var]) { + $classVar = '_' . $var; + $headers[ucfirst($var)] = implode(', ', $this->_formatAddress($this->{$classVar})); + } + } + + if ($include['subject']) { + $headers['Subject'] = $this->_subject; + } + + return $headers; + } + +/** + * Format addresses + * + * @param array $address + * @return array + */ + protected function _formatAddress($address) { + $return = array(); + foreach ($address as $email => $alias) { + if ($email === $alias) { + $return[] = $email; + } else { + $return[] = sprintf('%s <%s>', $alias, $email); + } + } + return $return; } /** diff --git a/lib/Cake/tests/Case/Network/CakeEmailTest.php b/lib/Cake/tests/Case/Network/CakeEmailTest.php index bd6137d22..13a5a3a39 100644 --- a/lib/Cake/tests/Case/Network/CakeEmailTest.php +++ b/lib/Cake/tests/Case/Network/CakeEmailTest.php @@ -124,6 +124,36 @@ class CakeEmailTest extends CakeTestCase { * @return void */ public function testHeaders() { + $this->CakeEmail->setHeaders(array('X-Something' => 'nice')); + $expected = array( + 'X-Something' => 'nice', + 'X-Mailer' => 'CakePHP Email Component', + 'Date' => date(DATE_RFC2822) + ); + $this->assertIdentical($this->CakeEmail->getHeaders(), $expected); + + $this->CakeEmail->addHeaders(array('X-Something' => 'very nice', 'X-Other' => 'cool')); + $expected = array( + 'X-Something' => 'very nice', + 'X-Other' => 'cool', + 'X-Mailer' => 'CakePHP Email Component', + 'Date' => date(DATE_RFC2822) + ); + $this->assertIdentical($this->CakeEmail->getHeaders(), $expected); + + $this->CakeEmail->setFrom('cake@cakephp.org'); + $this->assertIdentical($this->CakeEmail->getHeaders(), $expected); + + $expected['From'] = 'cake@cakephp.org'; + $this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true)), $expected); + + $this->CakeEmail->setFrom('cake@cakephp.org', 'CakePHP'); + $expected['From'] = 'CakePHP '; + $this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true)), $expected); + + $this->CakeEmail->setTo(array('cake@cakephp.org', 'php@cakephp.org' => 'CakePHP')); + $expected['To'] = 'cake@cakephp.org, CakePHP '; + $this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected); } /**