diff --git a/lib/Cake/Controller/Component/EmailComponent.php b/lib/Cake/Controller/Component/EmailComponent.php index 5e292556a34..4fe0c9ddfc0 100644 --- a/lib/Cake/Controller/Component/EmailComponent.php +++ b/lib/Cake/Controller/Component/EmailComponent.php @@ -577,197 +577,4 @@ function _strip($value, $message = false) { return $value; } -/** - * Wrapper for PHP mail function used for sending out emails - * - * @return bool Success - * @access private - */ - function _mail() { - $header = implode($this->lineFeed, $this->_header); - $message = implode($this->lineFeed, $this->_message); - if (is_array($this->to)) { - $to = implode(', ', array_map(array($this, '_formatAddress'), $this->to)); - } else { - $to = $this->to; - } - if (ini_get('safe_mode')) { - return @mail($to, $this->_encode($this->subject), $message, $header); - } - return @mail($to, $this->_encode($this->subject), $message, $header, $this->additionalParams); - } - - -/** - * Helper method to get socket, overridden in tests - * - * @param array $config Config data for the socket. - * @return void - * @access protected - */ - function _getSocket($config) { - $this->_smtpConnection = new CakeSocket($config); - } - -/** - * Sends out email via SMTP - * - * @return bool Success - * @access private - */ - function _smtp() { - App::uses('CakeSocket', 'Network'); - - $defaults = array( - 'host' => 'localhost', - 'port' => 25, - 'protocol' => 'smtp', - 'timeout' => 30 - ); - $this->smtpOptions = array_merge($defaults, $this->smtpOptions); - $this->_getSocket($this->smtpOptions); - - if (!$this->_smtpConnection->connect()) { - $this->smtpError = $this->_smtpConnection->lastError(); - return false; - } elseif (!$this->_smtpSend(null, '220')) { - return false; - } - - $httpHost = env('HTTP_HOST'); - - if (isset($this->smtpOptions['client'])) { - $host = $this->smtpOptions['client']; - } elseif (!empty($httpHost)) { - list($host) = explode(':', $httpHost); - } else { - $host = 'localhost'; - } - - if (!$this->_smtpSend("EHLO {$host}", '250') && !$this->_smtpSend("HELO {$host}", '250')) { - return false; - } - - if (isset($this->smtpOptions['username']) && isset($this->smtpOptions['password'])) { - $authRequired = $this->_smtpSend('AUTH LOGIN', '334|503'); - if ($authRequired == '334') { - if (!$this->_smtpSend(base64_encode($this->smtpOptions['username']), '334')) { - return false; - } - if (!$this->_smtpSend(base64_encode($this->smtpOptions['password']), '235')) { - return false; - } - } elseif ($authRequired != '503') { - return false; - } - } - - if (!$this->_smtpSend('MAIL FROM: ' . $this->_formatAddress($this->from, true))) { - return false; - } - - if (!is_array($this->to)) { - $tos = array_map('trim', explode(',', $this->to)); - } else { - $tos = $this->to; - } - foreach ($tos as $to) { - if (!$this->_smtpSend('RCPT TO: ' . $this->_formatAddress($to, true))) { - return false; - } - } - - foreach ($this->cc as $cc) { - if (!$this->_smtpSend('RCPT TO: ' . $this->_formatAddress($cc, true))) { - return false; - } - } - foreach ($this->bcc as $bcc) { - if (!$this->_smtpSend('RCPT TO: ' . $this->_formatAddress($bcc, true))) { - return false; - } - } - - if (!$this->_smtpSend('DATA', '354')) { - return false; - } - - $header = implode("\r\n", $this->_header); - $message = implode("\r\n", $this->_message); - if (!$this->_smtpSend($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.")) { - return false; - } - $this->_smtpSend('QUIT', false); - - $this->_smtpConnection->disconnect(); - return true; - } - -/** - * 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 protected - */ - function _smtpSend($data, $checkCode = '250') { - if (!is_null($data)) { - $this->_smtpConnection->write($data . "\r\n"); - } - while ($checkCode !== false) { - $response = ''; - $startTime = time(); - while (substr($response, -2) !== "\r\n" && ((time() - $startTime) < $this->smtpOptions['timeout'])) { - $response .= $this->_smtpConnection->read(); - } - if (substr($response, -2) !== "\r\n") { - $this->smtpError = 'timeout'; - return false; - } - $response = end(explode("\r\n", rtrim($response, "\r\n"))); - - if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) { - if ($code[2] === '-') { - continue; - } - return $code[1]; - } - $this->smtpError = $response; - return false; - } - return true; - } - -/** - * Set as controller flash message a debug message showing current settings in component - * - * @return boolean Success - * @access private - */ - function _debug() { - $nl = "\n"; - $header = implode($nl, $this->_header); - $message = implode($nl, $this->_message); - $fm = '
';
-
-		if (is_array($this->to)) {
-			$to = implode(', ', array_map(array($this, '_formatAddress'), $this->to));
-		} else {
-			$to = $this->to;
-		}
-		$fm .= sprintf('%s %s%s', 'To:', $to, $nl);
-		$fm .= sprintf('%s %s%s', 'From:', $this->from, $nl);
-		$fm .= sprintf('%s %s%s', 'Subject:', $this->_encode($this->subject), $nl);
-		$fm .= sprintf('%s%3$s%3$s%s', 'Header:', $header, $nl);
-		$fm .= sprintf('%s%3$s%3$s%s', 'Parameters:', $this->additionalParams, $nl);
-		$fm .= sprintf('%s%3$s%3$s%s', 'Message:', $message, $nl);
-		$fm .= '
'; - - if (isset($this->Controller->Session)) { - $this->Controller->Session->setFlash($fm, 'default', null, 'email'); - return true; - } - return $fm; - } } diff --git a/lib/Cake/tests/Case/Controller/Component/EmailComponentTest.php b/lib/Cake/tests/Case/Controller/Component/EmailComponentTest.php index 01733c6b6e1..48d558ac24a 100644 --- a/lib/Cake/tests/Case/Controller/Component/EmailComponentTest.php +++ b/lib/Cake/tests/Case/Controller/Component/EmailComponentTest.php @@ -29,62 +29,6 @@ */ class EmailTestComponent extends EmailComponent { - var $smtpSend = ''; -/** - * smtpSend method override for testing - * - * @access public - * @return mixed - */ - function smtpSend($data, $code = '250') { - return parent::_smtpSend($data, $code); - } - -/** - * undocumented function - * - * @return void - */ - function _smtpSend($data, $code = '250') { - if ($this->_debug) { - $this->smtpSend .= $data . "\n"; - return true; - } - return parent::_smtpSend($data, $code); - } - -/** - * Convenience setter method for testing. - * - * @access public - * @return void - */ - function setConnectionSocket($socket) { - $this->_smtpConnection = $socket; - } - -/** - * Allows mocks to be used with tests. - * - * @param array $config - * @return void - */ - function _getSocket($config) { - if (empty($this->_smtpConnection)) { - parent::_getSocket($config); - } - } - -/** - * Convenience getter method for testing. - * - * @access public - * @return mixed - */ - function getConnectionSocket() { - return $this->_smtpConnection; - } - /** * Convenience getter for testing. * @@ -237,252 +181,6 @@ function __osFix($string) { return str_replace(array("\r\n", "\r"), "\n", $string); } -/** - * testSmtpConfig method - * - * @access public - * @return void - */ - function testSmtpConfig() { - if ($this->skipIf(!@fsockopen('localhost', 25, $err, $errstr, .01), '%s No SMTP server running on localhost')) { - return; - } - $this->Controller->EmailTest->delivery = 'smtp'; - $this->Controller->EmailTest->smtpOptions = array(); - $this->Controller->EmailTest->send('anything'); - $config = array( - 'host' => 'localhost', - 'port' => 25, - 'protocol' => 'smtp', - 'timeout' => 30 - ); - $this->assertEqual($config, $this->Controller->EmailTest->smtpOptions); - - $this->Controller->EmailTest->smtpOptions = array('port' => 80); - $this->Controller->EmailTest->send('anything'); - $config['port'] = 80; - $this->assertEqual($config, $this->Controller->EmailTest->smtpOptions); - } - -/** - * testBadSmtpSend method - * - * @access public - * @return void - */ - function testBadSmtpSend() { - if ($this->skipIf(!@fsockopen('localhost', 25, $err, $errstr, .01), '%s No SMTP server running on localhost')) { - return; - } - $this->Controller->EmailTest->smtpOptions['host'] = 'blah'; - $this->Controller->EmailTest->delivery = 'smtp'; - $this->assertFalse($this->Controller->EmailTest->send('Should not work')); - } - -/** - * testSmtpSend method - * - * @access public - * @return void - */ - function testSmtpSend() { - if ($this->skipIf(!@fsockopen('localhost', 25, $err, $errstr, .01), '%s No SMTP server running on localhost')) { - return; - } - - $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)); - } - -/** - * testSmtpEhlo method - * - * @access public - * @return void - */ - function testSmtpEhlo() { - if ($this->skipIf(!@fsockopen('localhost', 25, $err, $errstr, .01), '%s No SMTP server running on localhost')) { - return; - } - - $connection = new CakeSocket(array('protocol'=>'smtp', 'host' => 'localhost', 'port' => 25)); - $this->Controller->EmailTest->setConnectionSocket($connection); - $this->Controller->EmailTest->smtpOptions['timeout'] = 10; - $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 - * - * @access public - * @return void - */ - function testSmtpSendMultipleTo() { - if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) { - return; - } - $this->Controller->EmailTest->reset(); - $this->Controller->EmailTest->to = array('postmaster@localhost', 'root@localhost'); - $this->Controller->EmailTest->from = 'noreply@example.com'; - $this->Controller->EmailTest->subject = 'Cake SMTP multiple To test'; - $this->Controller->EmailTest->replyTo = 'noreply@example.com'; - $this->Controller->EmailTest->template = null; - $this->Controller->EmailTest->_debug = true; - $this->Controller->EmailTest->sendAs = 'text'; - $this->Controller->EmailTest->delivery = 'smtp'; - - $socket = $this->getMock('CakeSocket'); - $socket->expects($this->any()) - ->method('connect') - ->will($this->returnValue(true)); - $this->Controller->EmailTest->setConnectionSocket($socket); - - $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message')); - - $this->assertPattern('/EHLO localhost\n/', $this->Controller->EmailTest->smtpSend); - $this->assertPattern('/MAIL FROM: \n/', $this->Controller->EmailTest->smtpSend); - $this->assertPattern('/RCPT TO: \n/', $this->Controller->EmailTest->smtpSend); - $this->assertPattern('/RCPT TO: \n/', $this->Controller->EmailTest->smtpSend); - $this->assertPattern( - '/To: postmaster@localhost, root@localhost[\n\r]/', - $this->Controller->EmailTest->smtpSend - ); - } - -/** - * test sending smtp from a host using a port. - * - * @return void - */ - function testSmtpSendHostWithPort() { - $bkp = env('HTTP_HOST'); - $_SERVER['HTTP_HOST'] = 'localhost:8080'; - - $this->Controller->EmailTest->reset(); - $this->Controller->EmailTest->to = array('root@localhost'); - $this->Controller->EmailTest->from = 'noreply@example.com'; - $this->Controller->EmailTest->subject = 'Cake SMTP host test'; - $this->Controller->EmailTest->replyTo = 'noreply@example.com'; - $this->Controller->EmailTest->template = null; - $this->Controller->EmailTest->delivery = 'smtp'; - $this->Controller->EmailTest->sendAs = 'text'; - $this->Controller->EmailTest->_debug = true; - - $socket = $this->getMock('CakeSocket'); - $socket->expects($this->any()) - ->method('connect') - ->will($this->returnValue(true)); - - $this->Controller->EmailTest->setConnectionSocket($socket); - $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message')); - - $this->assertPattern('/EHLO localhost\n/', $this->Controller->EmailTest->smtpSend); - - $_SERVER['HTTP_HOST'] = $bkp; - } - -/** - * testAuthenticatedSmtpSend method - * - * @access public - * @return void - */ - function testAuthenticatedSmtpSend() { - if ($this->skipIf(!@fsockopen('localhost', 25, $err, $errstr, .01), '%s No SMTP server running on localhost')) { - return; - } - - $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->smtpOptions['username'] = 'test'; - $this->Controller->EmailTest->smtpOptions['password'] = 'testing'; - - $this->Controller->EmailTest->delivery = 'smtp'; - $result = $this->Controller->EmailTest->send('This is the body of the message'); - $code = substr($this->Controller->EmailTest->smtpError, 0, 3); - $this->skipIf(!$code, '%s Authentication not enabled on server'); - - $this->assertFalse($result); - $this->assertEqual($code, '535'); - } - /** * testSendFormats method * @@ -665,31 +363,6 @@ function testTemplateNestedElements() { $this->assertPattern('/http\:\/\/example\.com/', $result); } -/** - * testSmtpSendSocket method - * - * @access public - * @return void - */ - function testSmtpSendSocket() { - if ($this->skipIf(!@fsockopen('localhost', 25, $err, $errstr, .01), '%s No SMTP server running on localhost')) { - return; - } - - $socket = new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->Controller->EmailTest->smtpOptions)); - $this->Controller->EmailTest->setConnectionSocket($socket); - - $this->assertSame($this->Controller->EmailTest->getConnectionSocket(), $socket); - - $response = $this->Controller->EmailTest->smtpSend('HELO', '250'); - $this->assertPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError); - - $this->Controller->EmailTest->reset(); - $response = $this->Controller->EmailTest->smtpSend('HELO somehostname', '250'); - - $this->assertNoPattern('/501 Syntax: HELO hostname/', (string)$this->Controller->EmailTest->smtpError); - } - /** * testSendDebug method *