/** * 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.'); } } }