From d1f6f2339e9e4f2db56db14a3a6ad80d070dadda Mon Sep 17 00:00:00 2001 From: chartjes Date: Wed, 27 Jun 2007 15:12:48 +0000 Subject: [PATCH] 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 --- cake/libs/controller/components/email.php | 74 +++++++++++-------- .../libs/controller/components/email.test.php | 6 ++ 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/cake/libs/controller/components/email.php b/cake/libs/controller/components/email.php index 06472c805..6768dd906 100644 --- a/cake/libs/controller/components/email.php +++ b/cake/libs/controller/components/email.php @@ -216,6 +216,14 @@ class EmailComponent extends Object{ */ var $__message = null; +/** + * Variable that holds SMTP connection + * + * @var resource + * @access private + */ + var $__smtpConnection = null; + /** * Enter description here... * @@ -507,16 +515,14 @@ class EmailComponent extends Object{ * @access private */ function __smtp() { - $this-> smtpConnection = @fsockopen($this->smtpOptions['host'], $this->smtpOptions['port'], $errno, $errstr, $this->smtpOptions['timeout']); - $this->__getSmtpResponse(); - - if (!$this->smtpConnection) { + $response = $this->__smtpConnect($this->smtpOptions); + + if ($response['status'] == false) { + $this->smtpError = "{$response['errno']}: {$response['errstr']}"; return false; } - if (!$this->__sendData("HELO cake")) { - return false; - } + $this->__sendData("HELO cake\r\n", false); if (!$this->__sendData("MAIL FROM: {$this->from}\r\n")) { return false; @@ -526,52 +532,60 @@ class EmailComponent extends Object{ return false; } - $this->__sendData("DATA\r\n"); - - 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"); + $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); 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 */ function __getSmtpResponse() { - $response = @fgets($this->smtpConnection, 512); + $response = @fgets($this->__smtpConnection, 512); return $response; } /** * 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) { - @fputs($this->smtpConnection, $data); + function __sendData($data, $check = true) { + @fwrite($this->__smtpConnection, $data); $response = $this->__getSmtpResponse(); /** * If there is a 250 in the response code, that means * everything went ok */ - if (stristr($response, '250') !== false) { - echo "Failed while sending:
{$data}
"; + if ($check == true && !stristr($response, '250')) { $this->smtpError = $response; return false; - } else { - return false; } + + return true; } /** * Enter description here... diff --git a/cake/tests/cases/libs/controller/components/email.test.php b/cake/tests/cases/libs/controller/components/email.test.php index d133cedbc..8fe7c1d82 100644 --- a/cake/tests/cases/libs/controller/components/email.test.php +++ b/cake/tests/cases/libs/controller/components/email.test.php @@ -48,7 +48,13 @@ class EmailTest extends CakeTestCase { 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() { + $this->Controller->Email->reset(); $this->Controller->Email->to = 'chartjes@localhost'; $this->Controller->Email->subject = 'Cake SMTP test'; $this->Controller->Email->replyTo = 'noreply@example.com';