Skip to content

Commit

Permalink
Fixing issue where Date header would be missing from Emails sent by
Browse files Browse the repository at this point in the history
EmailComponent.
Adding user configurable field for date.
Test cases added.  Fixes #1304
  • Loading branch information
markstory committed Nov 23, 2010
1 parent baeef09 commit d173a18
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cake/libs/controller/components/email.php
Expand Up @@ -93,6 +93,15 @@ class EmailComponent extends Object{
* @access public
*/
var $bcc = array();
/**
* The date to put in the Date: header. This should be a date
* conformant with the RFC2822 standard. Leave null, to have
* today's date generated.
*
* @var string
*/
var $date = null;

/**
* The subject of the email
*
Expand Down Expand Up @@ -345,6 +354,7 @@ function reset() {
$this->bcc = array();
$this->subject = null;
$this->additionalParams = null;
$this->date = null;
$this->smtpError = null;
$this->attachments = array();
$this->__header = array();
Expand Down Expand Up @@ -470,6 +480,13 @@ function __createHeader() {
if ($this->delivery == 'smtp') {
$this->__header[] = 'Subject: ' . $this->__encode($this->subject);
}

$date = $this->date;
if ($date == false) {
$date = date(DATE_RFC2822);
}
$this->__header[] = 'Date: ' . $date;

$this->__header[] = 'X-Mailer: ' . $this->xMailer;

if (!empty($this->headers)) {
Expand Down
66 changes: 65 additions & 1 deletion cake/tests/cases/libs/controller/components/email.test.php
Expand Up @@ -315,6 +315,7 @@ function testSendFormats() {
$this->Controller->EmailTest->template = null;
$this->Controller->EmailTest->delivery = 'debug';

$date = date(DATE_RFC2822);
$message = <<<MSGBLOC
<pre>To: postmaster@localhost
From: noreply@example.com
Expand All @@ -323,6 +324,7 @@ function testSendFormats() {
From: noreply@example.com
Reply-To: noreply@example.com
Date: $date
X-Mailer: CakePHP Email Component
Content-Type: {CONTENTTYPE}
Content-Transfer-Encoding: 7bitParameters:
Expand Down Expand Up @@ -365,6 +367,7 @@ function testTemplates() {

$this->Controller->EmailTest->delivery = 'debug';

$date = date(DATE_RFC2822);
$header = <<<HEADBLOC
To: postmaster@localhost
From: noreply@example.com
Expand All @@ -373,6 +376,7 @@ function testTemplates() {
From: noreply@example.com
Reply-To: noreply@example.com
Date: $date
X-Mailer: CakePHP Email Component
Content-Type: {CONTENTTYPE}
Content-Transfer-Encoding: 7bitParameters:
Expand Down Expand Up @@ -500,12 +504,70 @@ function testSendDebug() {
$this->Controller->EmailTest->reset();
$this->Controller->EmailTest->to = 'postmaster@localhost';
$this->Controller->EmailTest->from = 'noreply@example.com';
$this->Controller->EmailTest->subject = 'Cake SMTP test';
$this->Controller->EmailTest->subject = 'Cake Debug Test';
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
$this->Controller->EmailTest->template = null;

$this->Controller->EmailTest->delivery = 'debug';
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$result = $this->Controller->Session->read('Message.email.message');

$this->assertPattern('/To: postmaster@localhost\n/', $result);
$this->assertPattern('/Subject: Cake Debug Test\n/', $result);
$this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
$this->assertPattern('/From: noreply@example.com\n/', $result);
$this->assertPattern('/Date: ' . date(DATE_RFC2822) . '\n/', $result);
$this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
$this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
$this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
$this->assertPattern('/This is the body of the message/', $result);
}
/**
* testContentArray method
*
* @access public
* @return void
*/
function testSendContentArray() {
$this->Controller->EmailTest->to = 'postmaster@localhost';
$this->Controller->EmailTest->from = 'noreply@example.com';
$this->Controller->EmailTest->subject = 'Cake Debug Test';
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
$this->Controller->EmailTest->template = null;
$this->Controller->EmailTest->delivery = 'debug';

$content = array('First line', 'Second line', 'Third line');
$this->assertTrue($this->Controller->EmailTest->send($content));
$result = $this->Controller->Session->read('Message.email.message');

$this->assertPattern('/To: postmaster@localhost\n/', $result);
$this->assertPattern('/Subject: Cake Debug Test\n/', $result);
$this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
$this->assertPattern('/From: noreply@example.com\n/', $result);
$this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
$this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
$this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
$this->assertPattern('/First line\n/', $result);
$this->assertPattern('/Second line\n/', $result);
$this->assertPattern('/Third line\n/', $result);
}

/**
* test setting a custom date.
*
* @return void
*/
function testDateProperty() {
$this->Controller->EmailTest->to = 'postmaster@localhost';
$this->Controller->EmailTest->from = 'noreply@example.com';
$this->Controller->EmailTest->subject = 'Cake Debug Test';
$this->Controller->EmailTest->date = 'Today!';
$this->Controller->EmailTest->template = null;
$this->Controller->EmailTest->delivery = 'debug';

$this->assertTrue($this->Controller->EmailTest->send('test message'));
$result = $this->Controller->Session->read('Message.email.message');
$this->assertPattern('/Date: Today!\n/', $result);
}
/**
* testContentStripping method
Expand Down Expand Up @@ -639,6 +701,7 @@ function testReset() {
$this->Controller->EmailTest->return = 'test.return@example.com';
$this->Controller->EmailTest->cc = array('cc1@example.com', 'cc2@example.com');
$this->Controller->EmailTest->bcc = array('bcc1@example.com', 'bcc2@example.com');
$this->Controller->EmailTest->date = 'Today!';
$this->Controller->EmailTest->subject = 'Test subject';
$this->Controller->EmailTest->additionalParams = 'X-additional-header';
$this->Controller->EmailTest->delivery = 'smtp';
Expand All @@ -656,6 +719,7 @@ function testReset() {
$this->assertNull($this->Controller->EmailTest->return);
$this->assertIdentical($this->Controller->EmailTest->cc, array());
$this->assertIdentical($this->Controller->EmailTest->bcc, array());
$this->assertNull($this->Controller->EmailTest->date);
$this->assertNull($this->Controller->EmailTest->subject);
$this->assertNull($this->Controller->EmailTest->additionalParams);
$this->assertIdentical($this->Controller->EmailTest->getHeaders(), array());
Expand Down

0 comments on commit d173a18

Please sign in to comment.