diff --git a/lib/Cake/Network/CakeEmail.php b/lib/Cake/Network/CakeEmail.php index 7d82a28ed..d1c6777ee 100644 --- a/lib/Cake/Network/CakeEmail.php +++ b/lib/Cake/Network/CakeEmail.php @@ -92,6 +92,13 @@ class CakeEmail { */ protected $_bcc = array(); +/** + * Message ID + * + * @var mixed True to generate, False to ignore, String with value + */ + protected $_messageId = true; + /** * The subject of the email * @@ -531,6 +538,13 @@ class CakeEmail { if (!isset($headers['Date'])) { $headers['Date'] = date(DATE_RFC2822); } + if ($this->_messageId !== false) { + if ($this->_messageId === true) { + $headers['Message-ID'] = '<' . String::UUID() . '@' . env('HTTP_HOST') . '>'; + } else { + $headers['Message-ID'] = $this->_messageId; + } + } $relation = array( 'from' => 'From', @@ -615,6 +629,24 @@ class CakeEmail { $this->_transportName = (string)$name; } +/** + * Set Message-ID + * + * @param mixed $message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID + * @return void + * @thrown SocketException + */ + public function setMessageID($message) { + if (is_bool($message)) { + $this->_messageId = $message; + } else { + if (!preg_match('/^\<.+@.+\>$/', $message)) { + throw new SocketException(__('Invalid format to Message-ID. The text should be something like ""')); + } + $this->_messageId = $message; + } + } + /** * Set attachments * @@ -678,6 +710,7 @@ class CakeEmail { $this->_returnPath = array(); $this->_cc = array(); $this->_bcc = array(); + $this->_messageId = true; $this->_subject = ''; $this->_headers = array(); $this->_layout = 'default'; diff --git a/lib/Cake/tests/Case/Network/CakeEmailTest.php b/lib/Cake/tests/Case/Network/CakeEmailTest.php index a3eae184d..5ee7d18b0 100644 --- a/lib/Cake/tests/Case/Network/CakeEmailTest.php +++ b/lib/Cake/tests/Case/Network/CakeEmailTest.php @@ -139,7 +139,7 @@ class CakeEmailTest extends CakeTestCase { * * @dataProvider invalidEmails * @expectedException SocketException - * return void + * @return void */ public function testInvalidEmail($value) { $this->CakeEmail->setTo($value); @@ -168,6 +168,35 @@ class CakeEmailTest extends CakeTestCase { $this->assertIdentical($result, $expected); } +/** + * testMessageId method + * + * @return void + */ + public function testMessageId() { + $this->CakeEmail->setMessageId(true); + $result = $this->CakeEmail->getHeaders(); + $this->assertTrue(isset($result['Message-ID'])); + + $this->CakeEmail->setMessageId(false); + $result = $this->CakeEmail->getHeaders(); + $this->assertFalse(isset($result['Message-ID'])); + + $this->CakeEmail->setMessageId(''); + $result = $this->CakeEmail->getHeaders(); + $this->assertIdentical($result['Message-ID'], ''); + } + +/** + * testMessageIdInvalid method + * + * @return void + * @expectedException SocketException + */ + public function testMessageIdInvalid() { + $this->CakeEmail->setMessageId('my-email@localhost'); + } + /** * testSubject method * @@ -190,6 +219,7 @@ class CakeEmailTest extends CakeTestCase { * @return void */ public function testHeaders() { + $this->CakeEmail->setMessageId(false); $this->CakeEmail->setHeaders(array('X-Something' => 'nice')); $expected = array( 'X-Something' => 'nice',