Implemented method to set/get Message-ID.

This commit is contained in:
Juan Basso 2011-03-01 20:57:45 -03:00
parent 30dced741d
commit ace425892d
2 changed files with 64 additions and 1 deletions

View file

@ -92,6 +92,13 @@ class CakeEmail {
*/ */
protected $_bcc = array(); protected $_bcc = array();
/**
* Message ID
*
* @var mixed True to generate, False to ignore, String with value
*/
protected $_messageId = true;
/** /**
* The subject of the email * The subject of the email
* *
@ -531,6 +538,13 @@ class CakeEmail {
if (!isset($headers['Date'])) { if (!isset($headers['Date'])) {
$headers['Date'] = date(DATE_RFC2822); $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( $relation = array(
'from' => 'From', 'from' => 'From',
@ -615,6 +629,24 @@ class CakeEmail {
$this->_transportName = (string)$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 * Set attachments
* *
@ -678,6 +710,7 @@ class CakeEmail {
$this->_returnPath = array(); $this->_returnPath = array();
$this->_cc = array(); $this->_cc = array();
$this->_bcc = array(); $this->_bcc = array();
$this->_messageId = true;
$this->_subject = ''; $this->_subject = '';
$this->_headers = array(); $this->_headers = array();
$this->_layout = 'default'; $this->_layout = 'default';

View file

@ -139,7 +139,7 @@ class CakeEmailTest extends CakeTestCase {
* *
* @dataProvider invalidEmails * @dataProvider invalidEmails
* @expectedException SocketException * @expectedException SocketException
* return void * @return void
*/ */
public function testInvalidEmail($value) { public function testInvalidEmail($value) {
$this->CakeEmail->setTo($value); $this->CakeEmail->setTo($value);
@ -168,6 +168,35 @@ class CakeEmailTest extends CakeTestCase {
$this->assertIdentical($result, $expected); $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 * testSubject method
* *
@ -190,6 +219,7 @@ class CakeEmailTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testHeaders() { public function testHeaders() {
$this->CakeEmail->setMessageId(false);
$this->CakeEmail->setHeaders(array('X-Something' => 'nice')); $this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
$expected = array( $expected = array(
'X-Something' => 'nice', 'X-Something' => 'nice',