Skip to content

Commit

Permalink
domain configurable for mailing in CLI environment
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Apr 20, 2012
1 parent 21ba5bf commit a5c3230
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
31 changes: 29 additions & 2 deletions lib/Cake/Network/Email/CakeEmail.php
Expand Up @@ -139,6 +139,14 @@ class CakeEmail {
* @var mixed True to generate, False to ignore, String with value
*/
protected $_messageId = true;

/**
* Domain for messageId generation.
* Needs to be manually set for CLI mailing as env('HTTP_HOST') is empty
*
* @var string
*/
protected $_domain = null;

/**
* The subject of the email
Expand Down Expand Up @@ -308,6 +316,11 @@ public function __construct($config = null) {
if ($this->_appCharset !== null) {
$this->charset = $this->_appCharset;
}
$this->_domain = env('HTTP_HOST');
if (empty($this->_domain)) {
$this->_domain = php_uname('n');
}

if ($config) {
$this->config($config);
}
Expand Down Expand Up @@ -689,7 +702,7 @@ public function getHeaders($include = array()) {
}
if ($this->_messageId !== false) {
if ($this->_messageId === true) {
$headers['Message-ID'] = '<' . str_replace('-', '', String::UUID()) . '@' . env('HTTP_HOST') . '>';
$headers['Message-ID'] = '<' . str_replace('-', '', String::UUID()) . '@' . $this->_domain . '>';
} else {
$headers['Message-ID'] = $this->_messageId;
}
Expand Down Expand Up @@ -887,6 +900,20 @@ public function messageId($message = null) {
return $this;
}

/**
* Domain as top level (the part after @)
*
* @param string $domain Manually set the domain for CLI mailing
* @return mixed
*/
public function domain($domain = null) {
if ($domain === null) {
return $this->_domain;
}
$this->_domain = $domain;
return $this;
}

/**
* Add attachments to the email message
*
Expand Down Expand Up @@ -1097,7 +1124,7 @@ protected function _applyConfig($config) {
}
$simpleMethods = array(
'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
'messageId', 'subject', 'viewRender', 'viewVars', 'attachments',
'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
'transport', 'emailFormat', 'theme',
);
foreach ($simpleMethods as $method) {
Expand Down
32 changes: 32 additions & 0 deletions lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
Expand Up @@ -388,6 +388,38 @@ public function testMessageIdInvalid() {
$this->CakeEmail->messageId('my-email@localhost');
}

/**
* testDomain method
*
* @return void
*/
public function testDomain() {
$result = $this->CakeEmail->domain();
$expected = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
$this->assertSame($expected, $result);

$this->CakeEmail->domain('example.org');
$result = $this->CakeEmail->domain();
$expected = 'example.org';
$this->assertSame($expected, $result);
}

/**
* testMessageIdWithDomain method
*
* @return void
*/
public function testMessageIdWithDomain() {
$result = $this->CakeEmail->getHeaders();
$expected = '@' . (env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n')) . '>';
$this->assertTextContains($expected, $result['Message-ID']);

$this->CakeEmail->domain('example.org');
$result = $this->CakeEmail->getHeaders();
$expected = '@example.org>';
$this->assertTextContains($expected, $result['Message-ID']);
}

/**
* testSubject method
*
Expand Down

0 comments on commit a5c3230

Please sign in to comment.