示例#1
0
 /**
  * Protected method for sending data to SMTP connection
  *
  * @param string $data data to be sent to SMTP server
  * @param string|bool $checkCode code to check for in server response, false to skip
  * @return string|null The matched code, or null if nothing matched
  * @throws \Cake\Network\Exception\SocketException
  */
 protected function _smtpSend($data, $checkCode = '250')
 {
     $this->_lastResponse = [];
     if ($data !== null) {
         $this->_socket->write($data . "\r\n");
     }
     $timeout = $this->_config['timeout'];
     while ($checkCode !== false) {
         $response = '';
         $startTime = time();
         while (substr($response, -2) !== "\r\n" && time() - $startTime < $timeout) {
             $response .= $this->_socket->read();
         }
         if (substr($response, -2) !== "\r\n") {
             throw new SocketException('SMTP timeout.');
         }
         $responseLines = explode("\r\n", rtrim($response, "\r\n"));
         $response = end($responseLines);
         $this->_bufferResponseLines($responseLines);
         if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) {
             if ($code[2] === '-') {
                 continue;
             }
             return $code[1];
         }
         throw new SocketException(sprintf('SMTP Error: %s', $response));
     }
 }
示例#2
0
 /**
  * test configuring the context from the flat keys.
  *
  * @return void
  */
 public function testConfigContext()
 {
     $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
     $this->skipIf(!empty(getenv('http_proxy')) || !empty(getenv('https_proxy')), 'Proxy detected and cannot test SSL.');
     $config = ['host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5, 'ssl_verify_peer' => true, 'ssl_allow_self_signed' => false, 'ssl_verify_depth' => 5, 'ssl_verify_host' => true];
     $socket = new Socket($config);
     $socket->connect();
     $result = $socket->context();
     $this->assertTrue($result['ssl']['verify_peer']);
     $this->assertFalse($result['ssl']['allow_self_signed']);
     $this->assertEquals(5, $result['ssl']['verify_depth']);
     $this->assertEquals('smtp.gmail.com', $result['ssl']['CN_match']);
     $this->assertArrayNotHasKey('ssl_verify_peer', $socket->config());
     $this->assertArrayNotHasKey('ssl_allow_self_signed', $socket->config());
     $this->assertArrayNotHasKey('ssl_verify_host', $socket->config());
     $this->assertArrayNotHasKey('ssl_verify_depth', $socket->config());
 }
 /**
  * testReset method
  *
  * @return void
  */
 public function testReset()
 {
     $config = ['persistent' => true, 'host' => '127.0.0.1', 'protocol' => 'udp', 'port' => 80, 'timeout' => 20];
     $anotherSocket = new Socket($config);
     $anotherSocket->reset();
     $expected = ['persistent' => false, 'host' => 'localhost', 'protocol' => 'tcp', 'port' => 80, 'timeout' => 30];
     $this->assertEquals($expected, $anotherSocket->config(), 'Reset should cause config to return the defaults defined in _defaultConfig');
 }