Refactored SMTP send code, cleaned up tests. Test requires local SMTP server that is working

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5353 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
chartjes 2007-06-27 15:12:48 +00:00
parent 4573cf0796
commit d1f6f2339e
2 changed files with 50 additions and 30 deletions

View file

@ -216,6 +216,14 @@ class EmailComponent extends Object{
*/ */
var $__message = null; var $__message = null;
/**
* Variable that holds SMTP connection
*
* @var resource
* @access private
*/
var $__smtpConnection = null;
/** /**
* Enter description here... * Enter description here...
* *
@ -507,16 +515,14 @@ class EmailComponent extends Object{
* @access private * @access private
*/ */
function __smtp() { function __smtp() {
$this-> smtpConnection = @fsockopen($this->smtpOptions['host'], $this->smtpOptions['port'], $errno, $errstr, $this->smtpOptions['timeout']); $response = $this->__smtpConnect($this->smtpOptions);
$this->__getSmtpResponse();
if (!$this->smtpConnection) { if ($response['status'] == false) {
$this->smtpError = "{$response['errno']}: {$response['errstr']}";
return false; return false;
} }
if (!$this->__sendData("HELO cake")) { $this->__sendData("HELO cake\r\n", false);
return false;
}
if (!$this->__sendData("MAIL FROM: {$this->from}\r\n")) { if (!$this->__sendData("MAIL FROM: {$this->from}\r\n")) {
return false; return false;
@ -526,52 +532,60 @@ class EmailComponent extends Object{
return false; return false;
} }
$this->__sendData("DATA\r\n"); $this->__sendData("DATA\r\n{$this->__header}\r\n{$this->__message}\r\n\r\n\r\n.\r\n", false);
$this->__sendData("QUIT\r\n", false);
if (!$this->__sendData("To: {$this->to}\r\n")) {
return false;
}
if (!$this->__sendData("{$this->__header}\r\n")) {
return false;
}
if (!$this->__sendData("{$this->__message}\r\n")) {
return false;
}
$this->__sendData("QUIT\r\n");
return true; return true;
} }
/**
* Private method for connecting to an SMTP server
*
* @access private
* @param array $options SMTP connection options
* @return array
*/
function __smtpConnect($options) {
$status = true;
$this->__smtpConnection = @fsockopen($options['host'], $options['port'], $errno, $errstr, $options['timeout']);
if ($this->__smtpConnection == false) {
$status = false;
}
return array('status' => $status,
'errno' => $errno,
'errstr' => $errstr);
}
/** /**
* Private method for getting SMTP response * Private method for getting SMTP response
*/ */
function __getSmtpResponse() { function __getSmtpResponse() {
$response = @fgets($this->smtpConnection, 512); $response = @fgets($this->__smtpConnection, 512);
return $response; return $response;
} }
/** /**
* Private method for sending data to SMTP connection * Private method for sending data to SMTP connection
* *
* @param string data * @param string $data data to be sent to SMTP server
* @param boolean $check check for response from server
*/ */
function __sendData($data) { function __sendData($data, $check = true) {
@fputs($this->smtpConnection, $data); @fwrite($this->__smtpConnection, $data);
$response = $this->__getSmtpResponse(); $response = $this->__getSmtpResponse();
/** /**
* If there is a 250 in the response code, that means * If there is a 250 in the response code, that means
* everything went ok * everything went ok
*/ */
if (stristr($response, '250') !== false) { if ($check == true && !stristr($response, '250')) {
echo "Failed while sending:<pre>{$data}</pre>";
$this->smtpError = $response; $this->smtpError = $response;
return false; return false;
} else {
return false;
} }
return true;
} }
/** /**
* Enter description here... * Enter description here...

View file

@ -48,7 +48,13 @@ class EmailTest extends CakeTestCase {
ClassRegistry::addObject('view', new View($this->Controller)); ClassRegistry::addObject('view', new View($this->Controller));
} }
function testBadSmtpSend() {
$this->Controller->Email->smtpOptions['host'] = 'blah';
$this->assertFalse($this->Controller->Email->send('Should not work'));
}
function testSmtpSend() { function testSmtpSend() {
$this->Controller->Email->reset();
$this->Controller->Email->to = 'chartjes@localhost'; $this->Controller->Email->to = 'chartjes@localhost';
$this->Controller->Email->subject = 'Cake SMTP test'; $this->Controller->Email->subject = 'Cake SMTP test';
$this->Controller->Email->replyTo = 'noreply@example.com'; $this->Controller->Email->replyTo = 'noreply@example.com';