wppaste
WordPress

SMTP::getSMTPConnection( string $host, int $port = null, int $timeout = 30, array $options = [] ): false|resource

Source
wp-includes/PHPMailer/SMTP.php:407
Create connection to the SMTP server.

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$hoststring
SMTP server IP or host name
$portintoptional
The port number to connect toDefault: null
$timeoutintoptional
How long to wait for the connection to openDefault: 30
$optionsarrayoptional
An array of options for stream_context_create()Default: []

Return value

false|resource

Performance profile

How much work a call to SMTP::getSMTPConnection() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9

Executed per call on PHP 8.5. The body compiles to 111.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost SMTP::getSMTPConnection() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always9call_user_func_array()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11195
8.511195
8.411195
8.311195
8.211195
8.11119518 more instructions than PHP 7.4
7.49343–575

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

  • \PHPMailer\PHPMailer\SMTP::edebug()
  • \PHPMailer\PHPMailer\SMTP::setError()

Source code

    protected function getSMTPConnection($host, $port = null, $timeout = 30, $options = [])    {        static $streamok;        //This is enabled by default since 5.0.0 but some providers disable it        //Check this once and cache the result        if (null === $streamok) {            $streamok = function_exists('stream_socket_client');        }         $errno = 0;        $errstr = '';        if ($streamok) {            $socket_context = stream_context_create($options);            set_error_handler(function () {                call_user_func_array([$this, 'errorHandler'], func_get_args());            });            $connection = 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            $this->edebug(                'Connection: stream_socket_client not available, falling back to fsockopen',                self::DEBUG_CONNECTION            );            set_error_handler(function () {                call_user_func_array([$this, 'errorHandler'], func_get_args());            });            $connection = fsockopen(                $host,                $port,                $errno,                $errstr,                $timeout            );        }        restore_error_handler();         //Verify we connected properly        if (!is_resource($connection)) {            $this->setError(                'Failed to connect to server',                '',                (string) $errno,                $errstr            );            $this->edebug(                'SMTP ERROR: ' . $this->error['error']                . ": $errstr ($errno)",                self::DEBUG_CLIENT            );             return false;        }         //SMTP server can take longer to respond, give longer timeout for first read        //Windows does not have support for this timeout function        if (strpos(PHP_OS, 'WIN') !== 0) {            $max = (int)ini_get('max_execution_time');            //Don't bother if unlimited, or if set_time_limit is disabled            if (0 !== $max && $timeout > $max && strpos(ini_get('disable_functions'), 'set_time_limit') === false) {                @set_time_limit($timeout);            }            stream_set_timeout($connection, $timeout, 0);        }         return $connection;    }

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/PHPMailer/SMTP.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.