Skip to content

Instantly share code, notes, and snippets.

@webnitros
Created May 3, 2019 15:19
Show Gist options
  • Save webnitros/096df2751b1550486e949ff63f5ebfd2 to your computer and use it in GitHub Desktop.
Save webnitros/096df2751b1550486e949ff63f5ebfd2 to your computer and use it in GitHub Desktop.
/**
* Connect to a POP3 server.
* @access public
* @param string $host
* @param integer|boolean $port
* @param integer $tval
* @return boolean
*/
public function connect($host, $port = false, $tval = 30)
{
// Are we already connected?
if ($this->connected) {
return true;
}
//On Windows this will raise a PHP Warning error if the hostname doesn't exist.
//Rather than suppress it with @fsockopen, capture it cleanly instead
set_error_handler(array($this, 'catchWarning'));
if (false === $port) {
$port = $this->POP3_PORT;
}
// connect to the POP3 server
$this->pop_conn = fsockopen(
$host, // POP3 Host
$port, // Port #
$errno, // Error Number
$errstr, // Error Message
$tval
); // Timeout (seconds)
// Restore the error handler
restore_error_handler();
// Did we connect?
if (false === $this->pop_conn) {
// It would appear not...
$this->setError(array(
'error' => "Failed to connect to server $host on port $port",
'errno' => $errno,
'errstr' => $errstr
));
return false;
}
// Increase the stream time-out
stream_set_timeout($this->pop_conn, $tval, 0);
// Get the POP3 server response
$response = $this->readResponse();
strtok($response, '<');
$this->_timestamp = strtok('>');
if (!strpos($this->_timestamp, '@')) {
$this->_timestamp = null;
} else {
$this->_timestamp = '<' . $this->_timestamp . '>';
}
/* if ($this->isTls) {
$this->request('STLS');
$result = stream_socket_enable_crypto($this->pop_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if (!$result) {
throw new RuntimeException('cannot enable TLS');
}
}*/
// The connection is established and the POP3 server is talking
$this->connected = true;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment