wppaste
WordPress

PHPMailer::smtpConnect( array $options = null ): bool

Source
wp-includes/PHPMailer/PHPMailer.php:2324
Initiate a connection to an SMTP server.

Description

Returns false if the operation failed.

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

$optionsarrayoptional
An array of options compatible with stream_context_create()Default: null

Return value

bool

Performance profile

How much work a call to PHPMailer::smtpConnect() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
60–179

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 271.

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 · 31 distinct outcomes

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

WhenInstructionsCalls it makes
$options !== null && !->connected()60–63->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), ->close()
$options !== null && !->connected()67–70->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), ->close(), ->getSmtpErrorMessage()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && $secure === false96–103->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ::lang()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect()119–144->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect()119–144->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && ->startTLS()127–152->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->startTLS(), ->hello()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && !->startTLS()127–152->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->startTLS(), ->getSmtpErrorMessage()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && ->startTLS()127–152->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->startTLS(), ->hello()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && !->startTLS()127–152->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->startTLS(), ->getSmtpErrorMessage()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl"130–150->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->getServerExt()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl"130–150->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->getServerExt()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && ->authenticate()135–160->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->authenticate()
19 further outcomes, up to 179 instructions
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && ->authenticate()135–160->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->authenticate()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && ->startTLS()138–158->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->getServerExt(), ->startTLS(), ->hello()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && !->startTLS()138–158->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->getServerExt(), ->startTLS(), ->getSmtpErrorMessage()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && ->startTLS()138–158->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->getServerExt(), ->startTLS(), ->hello()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && !->startTLS()138–158->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->getServerExt(), ->startTLS(), ->getSmtpErrorMessage()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && !->authenticate()140–165->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->authenticate(), ::lang()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && !->authenticate()140–165->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->authenticate(), ::lang()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && ->startTLS() && ->authenticate()143–168->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->startTLS(), ->hello(), ->authenticate()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && ->startTLS() && ->authenticate()143–168->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->startTLS(), ->hello(), ->authenticate()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && ->authenticate()146–166->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->getServerExt(), ->authenticate()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && ->authenticate()146–166->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->getServerExt(), ->authenticate()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && ->startTLS() && !->authenticate()148–173->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->startTLS(), ->hello(), ->authenticate(), ::lang()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && ->startTLS() && !->authenticate()148–173->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->startTLS(), ->hello(), ->authenticate(), ::lang()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && !->authenticate()151–171->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->getServerExt(), ->authenticate(), ::lang()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && !->authenticate()151–171->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->getServerExt(), ->authenticate(), ::lang()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && ->startTLS() && ->authenticate()154–174->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->getServerExt(), ->startTLS(), ->hello(), ->authenticate()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && ->startTLS() && ->authenticate()154–174->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->getServerExt(), ->startTLS(), ->hello(), ->authenticate()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && ->startTLS() && !->authenticate()159–179->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->hello(), ->getServerExt(), ->startTLS(), ->hello(), ->authenticate(), ::lang()
$options !== null && !->connected() && !$hosts && preg_match() && ::isValidHost() && ->connect() && $secure !== "ssl" && ->startTLS() && !->authenticate()159–179->connected(), ->setTimeout(), ->setDebugLevel(), ->setDebugOutput(), ->setVerp(), ->setSMTPUTF8(), explode(), preg_match(), ::isValidHost(), ->connect(), ->serverHostname(), ->hello(), ->getServerExt(), ->startTLS(), ->hello(), ->authenticate(), ::lang()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev27160–17933
8.527160–17933
8.427160–179336 fewer instructions than PHP 8.3
8.327760–18333
8.227760–18333
8.127760–18333
7.427760–18333

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 · 7

  • \PHPMailer\PHPMailer\PHPMailer::getSMTPInstance()
  • \PHPMailer\PHPMailer\PHPMailer::edebug()
  • \PHPMailer\PHPMailer\PHPMailer::lang()
  • static::isValidHost()
  • \PHPMailer\PHPMailer\Exception::__construct()
  • \PHPMailer\PHPMailer\PHPMailer::serverHostname()
  • \PHPMailer\PHPMailer\PHPMailer::getSmtpErrorMessage()

Source code

    public function smtpConnect($options = null)    {        if (null === $this->smtp) {            $this->smtp = $this->getSMTPInstance();        }         //If no options are provided, use whatever is set in the instance        if (null === $options) {            $options = $this->SMTPOptions;        }         //Already connected?        if ($this->smtp->connected()) {            return true;        }         $this->smtp->setTimeout($this->Timeout);        $this->smtp->setDebugLevel($this->SMTPDebug);        $this->smtp->setDebugOutput($this->Debugoutput);        $this->smtp->setVerp($this->do_verp);        $this->smtp->setSMTPUTF8($this->UseSMTPUTF8);        if ($this->Host === null) {            $this->Host = 'localhost';        }        $hosts = explode(';', $this->Host);        $lastexception = null;         foreach ($hosts as $hostentry) {            $hostinfo = [];            if (                !preg_match(                    '/^(?:(ssl|tls):\/\/)?(.+?)(?::(\d+))?$/',                    trim($hostentry),                    $hostinfo                )            ) {                $this->edebug(self::lang('invalid_hostentry') . ' ' . trim($hostentry));                //Not a valid host entry                continue;            }            //$hostinfo[1]: optional ssl or tls prefix            //$hostinfo[2]: the hostname            //$hostinfo[3]: optional port number            //The host string prefix can temporarily override the current setting for SMTPSecure            //If it's not specified, the default value is used             //Check the host name is a valid name or IP address before trying to use it            if (!static::isValidHost($hostinfo[2])) {                $this->edebug(self::lang('invalid_host') . ' ' . $hostinfo[2]);                continue;            }            $prefix = '';            $secure = $this->SMTPSecure;            $tls = (static::ENCRYPTION_STARTTLS === $this->SMTPSecure);            if ('ssl' === $hostinfo[1] || ('' === $hostinfo[1] && static::ENCRYPTION_SMTPS === $this->SMTPSecure)) {                $prefix = 'ssl://';                $tls = false; //Can't have SSL and TLS at the same time                $secure = static::ENCRYPTION_SMTPS;            } elseif ('tls' === $hostinfo[1]) {                $tls = true;                //TLS doesn't use a prefix                $secure = static::ENCRYPTION_STARTTLS;            }            //Do we need the OpenSSL extension?            $sslext = defined('OPENSSL_ALGO_SHA256');            if (static::ENCRYPTION_STARTTLS === $secure || static::ENCRYPTION_SMTPS === $secure) {                //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled                if (!$sslext) {                    throw new Exception(self::lang('extension_missing') . 'openssl', self::STOP_CRITICAL);                }            }            $host = $hostinfo[2];            $port = $this->Port;            if (                array_key_exists(3, $hostinfo) &&                is_numeric($hostinfo[3]) &&                $hostinfo[3] > 0 &&                $hostinfo[3] < 65536            ) {                $port = (int) $hostinfo[3];

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/PHPMailer.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.