Changes to in manipulation of header.

This commit is contained in:
Juan Basso 2011-03-01 15:16:50 -03:00
parent e6c8171d1f
commit 6930ba6207
2 changed files with 105 additions and 13 deletions

View file

@ -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;
}
/**

View file

@ -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 <cake@cakephp.org>';
$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 <php@cakephp.org>';
$this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
}
/**