diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index bd365bf97..fb821e164 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -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 @@ -308,6 +316,11 @@ class CakeEmail { 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); } @@ -689,7 +702,7 @@ class CakeEmail { } 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; } @@ -887,6 +900,20 @@ class CakeEmail { 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 * @@ -1097,7 +1124,7 @@ class CakeEmail { } $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) { diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index 424d8b02e..45c668688 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -388,6 +388,38 @@ class CakeEmailTest extends CakeTestCase { $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 *