Skip to content

Commit

Permalink
Support to EHLO in SMTP server for EmailComponent. Fixes #54, #712, #737
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed May 25, 2010
1 parent 2d44929 commit bc3e745
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cake/libs/controller/components/email.php
Expand Up @@ -808,7 +808,7 @@ function _smtp() {
$host = 'localhost';
}

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

Expand Down Expand Up @@ -868,22 +868,26 @@ function _smtp() {
}

/**
* Private method for sending data to SMTP connection
* Protected method for sending data to SMTP connection
*
* @param string $data data to be sent to SMTP server
* @param mixed $checkCode code to check for in server response, false to skip
* @return bool Success
* @access private
* @access protected
*/
function _smtpSend($data, $checkCode = '250') {
if (!is_null($data)) {
$this->__smtpConnection->write($data . "\r\n");
}
if ($checkCode !== false) {
while ($checkCode !== false) {
$response = $this->__smtpConnection->read();
$response = end(explode("\r\n", rtrim($response, "\r\n")));

if (preg_match('/^(' . $checkCode . ')/', $response, $code)) {
return $code[0];
if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) {
if ($code[2] === '-') {
continue;
}
return $code[1];
}
$this->smtpError = $response;
return false;
Expand Down
55 changes: 55 additions & 0 deletions cake/tests/cases/libs/controller/components/email.test.php
Expand Up @@ -295,6 +295,61 @@ function testSmtpSend() {
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
}

/**
* testSmtpEhlo method
*
* @access public
* @return void
*/
function testSmtpEhlo() {
if (!$this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
return;
}

$connection =& new CakeSocket(array('protocol'=>'smtp', 'host' => 'localhost', 'port' => 25));
$this->Controller->EmailTest->setConnectionSocket($connection);
$this->assertTrue($connection->connect());
$this->assertTrue($this->Controller->EmailTest->smtpSend(null, '220') !== false);
$this->skipIf($this->Controller->EmailTest->smtpSend('EHLO locahost', '250') === false, '%s do not support EHLO.');
$connection->disconnect();

$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
Timeout: 30
To: postmaster@localhost
From: noreply@example.com
Subject: Cake SMTP test
Header:
To: postmaster@localhost
From: noreply@example.com
Reply-To: noreply@example.com
Subject: Cake SMTP test
X-Mailer: CakePHP Email Component
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bitParameters:
Message:
This is the body of the message
</pre>
TEMPDOC;
$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));
}

/**
* testSmtpSendMultipleTo method
Expand Down

0 comments on commit bc3e745

Please sign in to comment.