Skip to content

Commit

Permalink
Fall back to fsockopen if stream_socket_client is disabled, fixes PHP…
Browse files Browse the repository at this point in the history
  • Loading branch information
Synchro committed Jun 13, 2014
1 parent 6175e1f commit 7c74d52
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions class.smtp.php
Expand Up @@ -182,6 +182,12 @@ protected function edebug($str)
*/
public function connect($host, $port = null, $timeout = 30, $options = array())
{
static $streamok;
//This is enabled by default since 5.0.0 but some providers disable it
//Check this once and cache the result
if (is_null($streamok)) {
$streamok = function_exists('stream_socket_client');
}
// Clear errors to avoid confusion
$this->error = array();
// Make sure we are __not__ connected
Expand All @@ -199,16 +205,30 @@ public function connect($host, $port = null, $timeout = 30, $options = array())
}
$errno = 0;
$errstr = '';
$socket_context = stream_context_create($options);
//Suppress errors; connection failures are handled at a higher level
$this->smtp_conn = @stream_socket_client(
$host . ":" . $port,
$errno,
$errstr,
$timeout,
STREAM_CLIENT_CONNECT,
$socket_context
);
if ($streamok) {
$socket_context = stream_context_create($options);
//Suppress errors; connection failures are handled at a higher level
$this->smtp_conn = @stream_socket_client(
$host . ":" . $port,
$errno,
$errstr,
$timeout,
STREAM_CLIENT_CONNECT,
$socket_context
);
} else {
//Fall back to fsockopen which should work in more places, but is missing some features
if ($this->do_debug >= 3) {
$this->edebug("Connection: stream_socket_client not available, falling back to fsockopen");
}
$this->smtp_conn = fsockopen(
$host,
$port,
$errno,
$errstr,
$timeout
);
}
// Verify we connected properly
if (!is_resource($this->smtp_conn)) {
$this->error = array(
Expand Down

0 comments on commit 7c74d52

Please sign in to comment.