novalidatecert = $novalidatecert; return $this; } /** * Should we validate SSL certificate? */ public function validateCert(): bool { return ! $this->novalidatecert; } /** * Prepare socket options * * @return array */ private function prepareSocketOptions(): array { return $this->novalidatecert ? [ 'ssl' => [ 'verify_peer_name' => false, 'verify_peer' => false, ], ] : []; } /** * Setup connection socket * * @param string $host hostname or IP address of IMAP server * @param int|null $port of IMAP server, default is 143 (993 for ssl) * @param int $timeout timeout in seconds for initiating session * @return resource The socket created. * @throws Exception\RuntimeException If unable to connect to host. */ protected function setupSocket( string $transport, string $host, ?int $port, int $timeout ) { ErrorHandler::start(); $socket = stream_socket_client( sprintf('%s://%s:%d', $transport, $host, $port), $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, stream_context_create($this->prepareSocketOptions()) ); $error = ErrorHandler::stop(); if (! $socket) { throw new Exception\RuntimeException(sprintf( 'cannot connect to host%s', $error ? sprintf('; error = %s (errno = %d )', $error->getMessage(), $error->getCode()) : '' ), 0, $error); } if (false === stream_set_timeout($socket, $timeout)) { throw new Exception\RuntimeException('Could not set stream timeout'); } return $socket; } }