diff --git a/cake/libs/controller/components/email.php b/cake/libs/controller/components/email.php index 94ef82a3b34..d18784abb08 100755 --- a/cake/libs/controller/components/email.php +++ b/cake/libs/controller/components/email.php @@ -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; } @@ -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; diff --git a/cake/tests/cases/libs/controller/components/email.test.php b/cake/tests/cases/libs/controller/components/email.test.php index 1267c96a719..f5de3e9d2ce 100755 --- a/cake/tests/cases/libs/controller/components/email.test.php +++ b/cake/tests/cases/libs/controller/components/email.test.php @@ -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 = <<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 + + +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