From a5c323064f97c48f9dd84e76c2bb93b8bf4db7ae Mon Sep 17 00:00:00 2001 From: euromark Date: Fri, 20 Apr 2012 03:38:26 +0200 Subject: [PATCH] domain configurable for mailing in CLI environment --- lib/Cake/Network/Email/CakeEmail.php | 31 ++++++++++++++++-- .../Test/Case/Network/Email/CakeEmailTest.php | 32 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index bd365bf9742..fb821e16420 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 @@ 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); } @@ -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; } @@ -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 * @@ -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) { diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index 424d8b02ee1..45c66868812 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -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 *