Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding a tls option to SmtpTransport to support TLS SMTP servers like…

… MS Exchange 2010
  • Loading branch information...
commit 55c92065c674a9241b2ee36c32865a85b75d4443 1 parent 27a895d
@steinkel steinkel authored
View
13 lib/Cake/Network/Email/SmtpTransport.php
@@ -79,7 +79,8 @@ public function config($config = array()) {
'timeout' => 30,
'username' => null,
'password' => null,
- 'client' => null
+ 'client' => null,
+ 'tls' => false
);
$this->_config = $config + $default;
}
@@ -107,7 +108,15 @@ protected function _connect() {
try {
$this->_smtpSend("EHLO {$host}", '250');
+ if ($this->_config['tls']) {
+ $this->_smtpSend("STARTTLS", '220');
+ $this->_socket->enableCrypto('tls');
+ $this->_smtpSend("EHLO {$host}", '250');
+ }
} catch (SocketException $e) {
+ if ($this->_config['tls']) {
+ throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.'));
+ }
try {
$this->_smtpSend("HELO {$host}", '250');
} catch (SocketException $e2) {
@@ -132,6 +141,8 @@ protected function _auth() {
if (!$this->_smtpSend(base64_encode($this->_config['password']), '235')) {
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the password.'));
}
+ } elseif ($authRequired == '504') {
+ throw new SocketException(__d('cake_dev', 'SMTP authentication method not allowed, check if SMTP server requires TLS'));
} elseif ($authRequired != '503') {
throw new SocketException(__d('cake_dev', 'SMTP does not require authentication.'));
}
View
66 lib/Cake/Test/Case/Network/Email/SmtpTransportTest.php
@@ -81,7 +81,7 @@ class SmtpTransportTest extends CakeTestCase {
*/
public function setUp() {
if (!class_exists('MockSocket')) {
- $this->getMock('CakeSocket', array('read', 'write', 'connect'), array(), 'MockSocket');
+ $this->getMock('CakeSocket', array('read', 'write', 'connect', 'enableCrypto'), array(), 'MockSocket');
}
$this->socket = new MockSocket();
@@ -106,6 +106,70 @@ public function testConnectEhlo() {
}
/**
+ * testConnectEhloTls method
+ *
+ * @return void
+ */
+ public function testConnectEhloTls() {
+ $this->SmtpTransport->config(array('tls' => true));
+ $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
+ $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
+ $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
+ $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
+ $this->socket->expects($this->at(5))->method('write')->with("STARTTLS\r\n");
+ $this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("220 Server ready\r\n"));
+ $this->socket->expects($this->at(8))->method('other')->with('tls')->will($this->returnValue(true));
+ $this->socket->expects($this->at(9))->method('write')->with("EHLO localhost\r\n");
+ $this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 Accepted\r\n"));
+ $this->SmtpTransport->connect();
+ }
+
+/**
+ * testConnectEhloTlsOnNonTlsServer method
+ *
+ * @return void
+ */
+ public function testConnectEhloTlsOnNonTlsServer() {
+ $this->SmtpTransport->config(array('tls' => true));
+ $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
+ $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
+ $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
+ $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
+ $this->socket->expects($this->at(5))->method('write')->with("STARTTLS\r\n");
+ $this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
+ $this->setExpectedException('SocketException');
+ $this->SmtpTransport->connect();
+ }
+
+/**
+ * testConnectEhloNoTlsOnRequiredTlsServer method
+ *
+ * @return void
+ */
+ public function testConnectEhloNoTlsOnRequiredTlsServer() {
+ $this->SmtpTransport->config(array('tls' => false, 'username' => 'user', 'password' => 'pass'));
+ $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
+ $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
+ $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
+ $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
+ $this->socket->expects($this->at(5))->method('read')->with("AUTH LOGIN\r\n");
+ $this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
+ $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized authentication type\r\n"));
+ $this->setExpectedException('SocketException');
+ $this->SmtpTransport->connect();
+ $this->SmtpTransport->auth();
+ }
+
+/**
* testConnectHelo method
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.