Skip to content

Commit

Permalink
Added support to sender email. Fixes #13.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Apr 20, 2011
1 parent baba9fd commit e0b4623
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/config/email.php.default
Expand Up @@ -59,6 +59,7 @@ class EmailConfig {

public $fast = array(
'from' => 'you@localhost',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
Expand Down
1 change: 1 addition & 0 deletions lib/Cake/Console/templates/skel/config/email.php.default
Expand Up @@ -59,6 +59,7 @@ class EmailConfig {

public $fast = array(
'from' => 'you@localhost',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
Expand Down
33 changes: 32 additions & 1 deletion lib/Cake/Network/CakeEmail.php
Expand Up @@ -80,6 +80,13 @@ class CakeEmail {
*/
protected $_from = array();

/**
* The sender email
*
* @var array();
*/
protected $_sender = array();

/**
* The email the recipient will reply to
*
Expand Down Expand Up @@ -279,6 +286,21 @@ public function from($email = null, $name = null) {
return $this->_setEmailSingle('_from', $email, $name, __d('cake', 'From requires only 1 email address.'));
}

/**
* Sender
*
* @param mixed $email
* @param string $name
* @return mixed
* @thrown SocketException
*/
public function sender($email = null, $name = null) {
if ($email === null) {
return $this->_sender;
}
return $this->_setEmailSingle('_sender', $email, $name, __d('cake', 'Sender requires only 1 email address.'));
}

/**
* Reply-To
*
Expand Down Expand Up @@ -550,6 +572,7 @@ public function addHeaders($headers) {
public function getHeaders($include = array()) {
$defaults = array(
'from' => false,
'sender' => false,
'replyTo' => false,
'readReceipt' => false,
'returnPath' => false,
Expand All @@ -573,6 +596,13 @@ public function getHeaders($include = array()) {
$headers[$header] = current($this->_formatAddress($this->{$var}));
}
}
if ($include['sender']) {
if (key($this->_sender) === key($this->_from)) {
$headers['Sender'] = '';
} else {
$headers['Sender'] = current($this->_formatAddress($this->_sender));
}
}

foreach (array('to', 'cc', 'bcc') as $var) {
if ($include[$var]) {
Expand Down Expand Up @@ -973,7 +1003,7 @@ public static function deliver($to = null, $subject = null, $message = null, $tr
*/
protected static function _applyConfig(CakeEmail $obj, $config) {
$simpleMethods = array(
'from', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
'messageId', 'subject', 'viewRender', 'viewVars', 'attachments',
'transport', 'emailFormat'
);
Expand Down Expand Up @@ -1007,6 +1037,7 @@ protected static function _applyConfig(CakeEmail $obj, $config) {
public function reset() {
$this->_to = array();
$this->_from = array();
$this->_sender = array();
$this->_replyTo = array();
$this->_readReceipt = array();
$this->_returnPath = array();
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Network/Email/MailTransport.php
Expand Up @@ -35,7 +35,7 @@ public function send(CakeEmail $email) {
if (isset($this->_config['eol'])) {
$eol = $this->_config['eol'];
}
$headers = $email->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true));
$headers = $email->getHeaders(array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true));
$to = $headers['To'];
unset($headers['To']);
$header = $this->_headersToString($headers, $eol);
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Network/Email/SmtpTransport.php
Expand Up @@ -158,7 +158,7 @@ protected function _sendRcpt() {
protected function _sendData() {
$this->_smtpSend('DATA', '354');

$headers = $this->_cakeEmail->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), true));
$headers = $this->_cakeEmail->getHeaders(array_fill_keys(array('from', 'sender', '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.");
Expand Down
23 changes: 23 additions & 0 deletions lib/Cake/tests/Case/Network/CakeEmailTest.php
Expand Up @@ -174,6 +174,29 @@ public function testFrom() {
$this->assertIdentical($this->CakeEmail, $result);
}

/**
* testSender method
*
* @return void
*/
public function testSender() {
$this->CakeEmail->reset();
$this->assertIdentical($this->CakeEmail->sender(), array());

$this->CakeEmail->sender('cake@cakephp.org', 'Name');
$expected = array('cake@cakephp.org' => 'Name');
$this->assertIdentical($this->CakeEmail->sender(), $expected);

$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
$this->assertIdentical($headers['From'], false);
$this->assertIdentical($headers['Sender'], 'Name <cake@cakephp.org>');

$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
$this->assertIdentical($headers['From'], 'CakePHP <cake@cakephp.org>');
$this->assertIdentical($headers['Sender'], '');
}

/**
* testTo method
*
Expand Down

0 comments on commit e0b4623

Please sign in to comment.