Skip to content

Commit

Permalink
Implemented method to set/get Message-ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Apr 13, 2011
1 parent 30dced7 commit ace4258
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
33 changes: 33 additions & 0 deletions lib/Cake/Network/CakeEmail.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -531,6 +538,13 @@ public function getHeaders($include = array()) {
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',
Expand Down Expand Up @@ -615,6 +629,24 @@ public function setTransport($name) {
$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 "<uuid@server.com>"'));
}
$this->_messageId = $message;
}
}

/**
* Set attachments
*
Expand Down Expand Up @@ -678,6 +710,7 @@ public function reset() {
$this->_returnPath = array();
$this->_cc = array();
$this->_bcc = array();
$this->_messageId = true;
$this->_subject = '';
$this->_headers = array();
$this->_layout = 'default';
Expand Down
32 changes: 31 additions & 1 deletion lib/Cake/tests/Case/Network/CakeEmailTest.php
Expand Up @@ -139,7 +139,7 @@ public static function invalidEmails() {
*
* @dataProvider invalidEmails
* @expectedException SocketException
* return void
* @return void
*/
public function testInvalidEmail($value) {
$this->CakeEmail->setTo($value);
Expand Down Expand Up @@ -168,6 +168,35 @@ public function testFormatAddress() {
$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('<my-email@localhost>');
$result = $this->CakeEmail->getHeaders();
$this->assertIdentical($result['Message-ID'], '<my-email@localhost>');
}

/**
* testMessageIdInvalid method
*
* @return void
* @expectedException SocketException
*/
public function testMessageIdInvalid() {
$this->CakeEmail->setMessageId('my-email@localhost');
}

/**
* testSubject method
*
Expand All @@ -190,6 +219,7 @@ public function testSubject() {
* @return void
*/
public function testHeaders() {
$this->CakeEmail->setMessageId(false);
$this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
$expected = array(
'X-Something' => 'nice',
Expand Down

0 comments on commit ace4258

Please sign in to comment.