示例#1
0
 /**
  * Connect to SMTP Server
  *
  * @return void
  * @throws \Cake\Network\Exception\SocketException
  */
 protected function _connect()
 {
     $this->_generateSocket();
     if (!$this->_socket->connect()) {
         throw new SocketException('Unable to connect to SMTP server.');
     }
     $this->_smtpSend(null, '220');
     $config = $this->_config;
     if (isset($config['client'])) {
         $host = $config['client'];
     } elseif ($httpHost = env('HTTP_HOST')) {
         list($host) = explode(':', $httpHost);
     } else {
         $host = 'localhost';
     }
     try {
         $this->_smtpSend("EHLO {$host}", '250');
         if ($config['tls']) {
             $this->_smtpSend("STARTTLS", '220');
             $this->_socket->enableCrypto('tls');
             $this->_smtpSend("EHLO {$host}", '250');
         }
     } catch (SocketException $e) {
         if ($config['tls']) {
             throw new SocketException('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) {
             throw new SocketException('SMTP server did not accept the connection.');
         }
     }
 }
示例#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());
 }