Skip to content

Commit

Permalink
Removing hard coded HELO hostname of 'cake' in EmailComponent; better…
Browse files Browse the repository at this point in the history
… compliance with RFC-821 3.5. Test cases added. Fixes #6264.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8152 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
jperras committed Apr 24, 2009
1 parent 82cb895 commit 650d1e7
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 86 deletions.
8 changes: 7 additions & 1 deletion cake/libs/controller/components/email.php
Expand Up @@ -673,7 +673,13 @@ function __smtp() {
return false;
}

if (!$this->__smtpSend('HELO cake', '250')) {
if (isset($this->smtpOptions['host'])) {
$host = $this->smtpOptions['host'];
} else {
$host = env('HTTP_HOST');
}

if (!$this->__smtpSend("HELO {$host}", '250')) {
return false;
}

Expand Down
230 changes: 145 additions & 85 deletions cake/tests/cases/libs/controller/components/email.test.php
Expand Up @@ -25,6 +25,41 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Component', 'Email');
/**
* EmailTestComponent class
*
* @package cake
* @subpackage cake.tests.cases.libs.controller.components
*/
class EmailTestComponent extends EmailComponent {
/**
* smtpSend method override for testing
*
* @access public
* @return mixed
*/
function smtpSend($data, $code = '250') {
return parent::__smtpSend($data, $code);
}
/**
* Convenience setter method for testing.
*
* @access public
* @return void
*/
function setConnectionSocket(&$socket) {
$this->__smtpConnection = $socket;
}
/**
* Convenience getter method for testing.
*
* @access public
* @return mixed
*/
function getConnectionSocket() {
return $this->__smtpConnection;
}
}
/**
* EmailTestController class
*
Expand Down Expand Up @@ -52,7 +87,7 @@ class EmailTestController extends Controller {
* @var array
* @access public
*/
var $components = array('Email');
var $components = array('EmailTest');
/**
* pageTitle property
*
Expand Down Expand Up @@ -98,7 +133,7 @@ function setUp() {
@$this->Controller->Component->init($this->Controller);
set_error_handler('simpleTestErrorHandler');

$this->Controller->Email->initialize($this->Controller, array());
$this->Controller->EmailTest->initialize($this->Controller, array());
ClassRegistry::addObject('view', new View($this->Controller));

$this->_viewPaths = Configure::read('viewPaths');
Expand All @@ -125,9 +160,9 @@ function tearDown() {
* @return void
*/
function testBadSmtpSend() {
$this->Controller->Email->smtpOptions['host'] = 'blah';
$this->Controller->Email->delivery = 'smtp';
$this->assertFalse($this->Controller->Email->send('Should not work'));
$this->Controller->EmailTest->smtpOptions['host'] = 'blah';
$this->Controller->EmailTest->delivery = 'smtp';
$this->assertFalse($this->Controller->EmailTest->send('Should not work'));
}
/**
* testSmtpSend method
Expand All @@ -139,18 +174,18 @@ function testSmtpSend() {
if (!$this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
return;
}
$this->Controller->Email->reset();
$this->Controller->Email->to = 'postmaster@localhost';
$this->Controller->Email->from = 'noreply@example.com';
$this->Controller->Email->subject = 'Cake SMTP test';
$this->Controller->Email->replyTo = 'noreply@example.com';
$this->Controller->Email->template = null;

$this->Controller->Email->delivery = 'smtp';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));

$this->Controller->Email->_debug = true;
$this->Controller->Email->sendAs = 'text';
$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->replyTo = 'noreply@example.com';
$this->Controller->EmailTest->template = null;

$this->Controller->EmailTest->delivery = 'smtp';
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));

$this->Controller->EmailTest->_debug = true;
$this->Controller->EmailTest->sendAs = 'text';
$expect = <<<TEMPDOC
<pre>Host: localhost
Port: 25
Expand All @@ -174,7 +209,7 @@ function testSmtpSend() {
</pre>
TEMPDOC;
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
}
/**
Expand All @@ -186,20 +221,22 @@ function testSmtpSend() {
function testAuthenticatedSmtpSend() {
$this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost');

$this->Controller->Email->reset();
$this->Controller->Email->to = 'postmaster@localhost';
$this->Controller->Email->from = 'noreply@example.com';
$this->Controller->Email->subject = 'Cake SMTP test';
$this->Controller->Email->replyTo = 'noreply@example.com';
$this->Controller->Email->template = null;
$this->Controller->Email->smtpOptions['username'] = 'test';
$this->Controller->Email->smtpOptions['password'] = 'testing';

$this->Controller->Email->delivery = 'smtp';
$result = $this->Controller->Email->send('This is the body of the message');
$code = substr($this->Controller->Email->smtpError, 0, 3);
$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->replyTo = 'noreply@example.com';
$this->Controller->EmailTest->template = null;
$this->Controller->EmailTest->smtpOptions['username'] = 'test';
$this->Controller->EmailTest->smtpOptions['password'] = 'testing';

$this->Controller->EmailTest->delivery = 'smtp';
$result = $this->Controller->EmailTest->send('This is the body of the message');
$code = substr($this->Controller->EmailTest->smtpError, 0, 3);
$this->skipIf(!$code, '%s Authentication not enabled on server');
$this->assertTrue(!$result && $code == '535');

$this->assertFalse($result);
$this->assertEqual($code, '535');
}
/**
* testSendFormats method
Expand All @@ -208,13 +245,13 @@ function testAuthenticatedSmtpSend() {
* @return void
*/
function testSendFormats() {
$this->Controller->Email->reset();
$this->Controller->Email->to = 'postmaster@localhost';
$this->Controller->Email->from = 'noreply@example.com';
$this->Controller->Email->subject = 'Cake SMTP test';
$this->Controller->Email->replyTo = 'noreply@example.com';
$this->Controller->Email->template = null;
$this->Controller->Email->delivery = 'debug';
$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->replyTo = 'noreply@example.com';
$this->Controller->EmailTest->template = null;
$this->Controller->EmailTest->delivery = 'debug';

$message = <<<MSGBLOC
<pre>To: postmaster@localhost
Expand All @@ -234,20 +271,20 @@ function testSendFormats() {
</pre>
MSGBLOC;
$this->Controller->Email->sendAs = 'text';
$this->Controller->EmailTest->sendAs = 'text';
$expect = str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $message);
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));

$this->Controller->Email->sendAs = 'html';
$this->Controller->EmailTest->sendAs = 'html';
$expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message);
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));

// TODO: better test for format of message sent?
$this->Controller->Email->sendAs = 'both';
$this->Controller->EmailTest->sendAs = 'both';
$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"' . "\n", $message);
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
}
/**
Expand All @@ -257,13 +294,13 @@ function testSendFormats() {
* @return void
*/
function testTemplates() {
$this->Controller->Email->reset();
$this->Controller->Email->to = 'postmaster@localhost';
$this->Controller->Email->from = 'noreply@example.com';
$this->Controller->Email->subject = 'Cake SMTP test';
$this->Controller->Email->replyTo = 'noreply@example.com';
$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->replyTo = 'noreply@example.com';

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

$header = <<<HEADBLOC
To: postmaster@localhost
Expand All @@ -282,8 +319,8 @@ function testTemplates() {
HEADBLOC;

$this->Controller->Email->layout = 'default';
$this->Controller->Email->template = 'default';
$this->Controller->EmailTest->layout = 'default';
$this->Controller->EmailTest->template = 'default';

$text = <<<TEXTBLOC
Expand All @@ -310,22 +347,22 @@ function testTemplates() {
HTMLBLOC;

$this->Controller->Email->sendAs = 'text';
$this->Controller->EmailTest->sendAs = 'text';
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));

$this->Controller->Email->sendAs = 'html';
$this->Controller->EmailTest->sendAs = 'html';
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));

$this->Controller->Email->sendAs = 'both';
$this->Controller->EmailTest->sendAs = 'both';
$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"' . "\n", $header);
$expect .= '--alt-' . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $text . "\n\n";
$expect .= '--alt-' . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $html . "\n\n";
$expect = '<pre>' . $expect . '--alt---' . "\n\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));

$html = <<<HTMLBLOC
Expand All @@ -344,9 +381,9 @@ function testTemplates() {
HTMLBLOC;

$this->Controller->Email->sendAs = 'html';
$this->Controller->EmailTest->sendAs = 'html';
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message', 'default', 'thin'));
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message', 'default', 'thin'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));

return;
Expand All @@ -362,27 +399,50 @@ function testTemplates() {
TEXTBLOC;

$this->Controller->Email->sendAs = 'text';
$this->Controller->EmailTest->sendAs = 'text';
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message', 'wide', 'default'));
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message', 'wide', 'default'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
}
/**
* testSmtpSendSocket method
*
* @access public
* @return void
*/
function testSmtpSendSocket() {
$this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost');

$this->Controller->EmailTest->reset();
$socket =& new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->Controller->EmailTest->smtpOptions));
$this->Controller->EmailTest->setConnectionSocket($socket);

$this->assertTrue($this->Controller->EmailTest->getConnectionSocket());

$response = $this->Controller->EmailTest->smtpSend('HELO', '250');
$this->assertPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError);

$this->Controller->EmailTest->smtpError = null;
$response = $this->Controller->EmailTest->smtpSend('HELO somehostname', '250');
$this->assertNoPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError);

}
/**
* testSendDebug method
*
* @access public
* @return void
*/
function testSendDebug() {
$this->Controller->Email->reset();
$this->Controller->Email->to = 'postmaster@localhost';
$this->Controller->Email->from = 'noreply@example.com';
$this->Controller->Email->subject = 'Cake SMTP test';
$this->Controller->Email->replyTo = 'noreply@example.com';
$this->Controller->Email->template = null;

$this->Controller->Email->delivery = 'debug';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$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->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'));
}
/**
* testContentStripping method
Expand All @@ -394,7 +454,7 @@ function testContentStripping() {
$content = "Previous content\n--alt-\nContent-TypeContent-Type:: text/html; charsetcharset==utf-8\nContent-Transfer-Encoding: 7bit";
$content .= "\n\n<p>My own html content</p>";

$result = $this->Controller->Email->__strip($content, true);
$result = $this->Controller->EmailTest->__strip($content, true);
$expected = "Previous content\n--alt-\n text/html; utf-8\n 7bit\n\n<p>My own html content</p>";
$this->assertEqual($result, $expected);
}
Expand All @@ -405,28 +465,28 @@ function testContentStripping() {
* @return void
*/
function testMultibyte() {
$this->Controller->Email->reset();
$this->Controller->Email->to = 'postmaster@localhost';
$this->Controller->Email->from = 'noreply@example.com';
$this->Controller->Email->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
$this->Controller->Email->replyTo = 'noreply@example.com';
$this->Controller->Email->template = null;
$this->Controller->Email->delivery = 'debug';
$this->Controller->EmailTest->reset();
$this->Controller->EmailTest->to = 'postmaster@localhost';
$this->Controller->EmailTest->from = 'noreply@example.com';
$this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
$this->Controller->EmailTest->template = null;
$this->Controller->EmailTest->delivery = 'debug';

$subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';

$this->Controller->Email->sendAs = 'text';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->Controller->EmailTest->sendAs = 'text';
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
$this->assertEqual(trim($matches[1]), $subject);

$this->Controller->Email->sendAs = 'html';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->Controller->EmailTest->sendAs = 'html';
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
$this->assertEqual(trim($matches[1]), $subject);

$this->Controller->Email->sendAs = 'both';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->Controller->EmailTest->sendAs = 'both';
$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
$this->assertEqual(trim($matches[1]), $subject);
}
Expand Down

0 comments on commit 650d1e7

Please sign in to comment.