From 15da6d12557e02b779dbe6a7d51f1bc3ed373af9 Mon Sep 17 00:00:00 2001 From: chartjes Date: Tue, 26 Jun 2007 20:29:38 +0000 Subject: [PATCH] Added code to allow Email compoenent to send email via SMTP, along with test for component git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5351 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/controller/components/email.php | 85 +++++++++++++++++-- .../libs/controller/components/email.test.php | 17 ++-- 2 files changed, 85 insertions(+), 17 deletions(-) diff --git a/cake/libs/controller/components/email.php b/cake/libs/controller/components/email.php index ff501e95e..06472c805 100644 --- a/cake/libs/controller/components/email.php +++ b/cake/libs/controller/components/email.php @@ -159,6 +159,13 @@ class EmailComponent extends Object{ 'host' => 'localhost', 'timeout' => 30); +/** + * SMTP errors variable + * + * @var string + * @access public + */ + var $smtpError = null; /** * Enter description here... * @@ -500,19 +507,71 @@ class EmailComponent extends Object{ * @access private */ function __smtp() { - $smtpConnect = fsockopen($this->smtpOptions['host'], $this->smtpOptions['port'], $errno, $errstr, $this->smtpOptions['timeout']); + $this-> smtpConnection = @fsockopen($this->smtpOptions['host'], $this->smtpOptions['port'], $errno, $errstr, $this->smtpOptions['timeout']); + $this->__getSmtpResponse(); - if (!$smtpConnect) { + if (!$this->smtpConnection) { return false; } - fputs($smtpConnect, "HELO {$this->smtpOptions['host']}" . $this->_newLine); - fputs($smtpConnect, "MAIL FROM: {$this->smtpOptions['from']}" . $this->_newLine); - fputs($smtpConnect, "RCPT TO: {$this->to}" . $this->_newLine); - fputs($smtpConnect, "DATE" . $this->_newLine); - fputs($smtpConnect, "To: {$this->to}\r\nFrom: {$this->smtpOptions['from']}\r\n{$this->subject}\r\n{$this->__header}\r\n\r\n{$this->__message}\r\n"); - fputs($smtpConnect, "QUIT" . $this->_newLine); - fclose($smtpConnect); + if (!$this->__sendData("HELO cake")) { + return false; + } + + if (!$this->__sendData("MAIL FROM: {$this->from}\r\n")) { + return false; + } + + if (!$this->__sendData("RCPT TO: {$this->to}\r\n")) { + 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"); + return true; + } + + /** + * Private method for getting SMTP response + */ + function __getSmtpResponse() { + $response = @fgets($this->smtpConnection, 512); + return $response; + } + + /** + * Private method for sending data to SMTP connection + * + * @param string data + */ + function __sendData($data) { + @fputs($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}
"; + $this->smtpError = $response; + return false; + } else { + return false; + } } /** * Enter description here... @@ -522,7 +581,15 @@ class EmailComponent extends Object{ */ function __debug() { $fm = '
';
+		
+		if ($this->delivery == 'smtp') {
+			$fm .= sprintf('%s %s', 'Host:', $this->smtpOptions['host']);
+			$fm .= sprintf('%s %s', 'Port:', $this->smtpOptions['port']);
+			$fm .= sprintf('%s %s', 'Timeout:', $this->smtpOptions['timeout']);
+		}
+
 		$fm .= sprintf('%s %s', 'To:', $this->to);
+		$fm .= sprintf('%s %s', 'From:', $this->from);
 		$fm .= sprintf('%s %s', 'Subject:', $this->subject);
 		$fm .= sprintf('%s\n\n%s', 'Header:', $this->__header);
 		$fm .= sprintf('%s\n\n%s', 'Parameters:', $this->additionalParams);
diff --git a/cake/tests/cases/libs/controller/components/email.test.php b/cake/tests/cases/libs/controller/components/email.test.php
index 21f227333..d133cedbc 100644
--- a/cake/tests/cases/libs/controller/components/email.test.php
+++ b/cake/tests/cases/libs/controller/components/email.test.php
@@ -48,14 +48,15 @@ class EmailTest extends CakeTestCase {
 		ClassRegistry::addObject('view', new View($this->Controller));
 	}
 
-	function testConstruction() {
-		$this->assertTrue(is_object($this->Controller->Email));
+	function testSmtpSend() {
+		$this->Controller->Email->to = 'chartjes@localhost';
+		$this->Controller->Email->subject = 'Cake SMTP test';
+		$this->Controller->Email->replyTo = 'noreply@example.com';
+		$this->Controller->Email->from = 'noreply@example.com';
+		$this->Controller->Email->delivery = 'smtp';
+		$this->Controller->Email->template = null;
+		$this->assertTrue($this->Controller->Email->send("This is the body of the message"));
 	}
 
-	function testBadSmtpSent() {
-		$this->Controller->Email->smtpOptions['host'] = 'caketest.com';
-		$this->Controller->Email->delivery = 'smtp';
-		$this->assertFalse($this->Controller->Email->send('This should not work'));
-	}
 }
-?>
\ No newline at end of file
+?>