Permalink
Browse files

Merge pull request #614 from dereuromark/2.2-email-domain

2.2 email domain
  • Loading branch information...
markstory committed Apr 20, 2012
2 parents 21ba5bf + a5c3230 commit a1f64b9080d531c5d522901ba6125d29802f378c
Showing with 61 additions and 2 deletions.
  1. +29 −2 lib/Cake/Network/Email/CakeEmail.php
  2. +32 −0 lib/Cake/Test/Case/Network/Email/CakeEmailTest.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) {
@@ -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
*

0 comments on commit a1f64b9

Please sign in to comment.