wppaste
WordPress

SMTP::connect( string $host, int $port = null, int $timeout = 30, array $options = [] ): bool

Source
wp-includes/PHPMailer/SMTP.php:348
Connect to an 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

bool

Performance profile

How much work a call to SMTP::connect() 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
14–81

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

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

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

WhenInstructionsCalls it makes
->connected()14->setError(), ->connected(), ->setError()
!->connected()41–43->setError(), ->connected(), ->edebug(), ->getSMTPConnection()
!->connected()46–48->setError(), ->connected(), var_export(), ->edebug(), ->getSMTPConnection()
!->connected() && $responseCode === 22063–65->setError(), ->connected(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug()
!->connected() && $responseCode === 22068–70->setError(), ->connected(), var_export(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug()
!->connected() && $responseCode !== 220 && $responseCode !== 55472–74->setError(), ->connected(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug(), ->edebug(), ->close()
!->connected() && $responseCode !== 220 && $responseCode === 55474–76->setError(), ->connected(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug(), ->quit(), ->edebug(), ->close()
!->connected() && $responseCode !== 220 && $responseCode !== 55477–79->setError(), ->connected(), var_export(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug(), ->edebug(), ->close()
!->connected() && $responseCode !== 220 && $responseCode === 55479–81->setError(), ->connected(), var_export(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug(), ->quit(), ->edebug(), ->close()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8714–8061 fewer instruction than PHP 8.5
8.58814–816
8.48814–8163 fewer instructions than PHP 8.3
8.39114–846
8.29114–846
8.19114–846
7.49114–846

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\SMTP::setError()
  • \PHPMailer\PHPMailer\SMTP::connected()
  • \PHPMailer\PHPMailer\SMTP::edebug()
  • \PHPMailer\PHPMailer\SMTP::getSMTPConnection()
  • \PHPMailer\PHPMailer\SMTP::get_lines()
  • \PHPMailer\PHPMailer\SMTP::quit()
  • \PHPMailer\PHPMailer\SMTP::close()

Source code

    public function connect($host, $port = null, $timeout = 30, $options = [])    {        //Clear errors to avoid confusion        $this->setError('');        //Make sure we are __not__ connected        if ($this->connected()) {            //Already connected, generate error            $this->setError('Already connected to a server');             return false;        }        if (empty($port)) {            $port = self::DEFAULT_PORT;        }        //Connect to the SMTP server        $this->edebug(            "Connection: opening to $host:$port, timeout=$timeout, options=" .            (count($options) > 0 ? var_export($options, true) : 'array()'),            self::DEBUG_CONNECTION        );         $this->smtp_conn = $this->getSMTPConnection($host, $port, $timeout, $options);         if ($this->smtp_conn === false) {            //Error info already set inside `getSMTPConnection()`            return false;        }         $this->edebug('Connection: opened', self::DEBUG_CONNECTION);         //Get any announcement        $this->last_reply = $this->get_lines();        $this->edebug('SERVER -> CLIENT: ' . $this->last_reply, self::DEBUG_SERVER);        $responseCode = (int)substr($this->last_reply, 0, 3);        if ($responseCode === 220) {            return true;        }        //Anything other than a 220 response means something went wrong        //RFC 5321 says the server will wait for us to send a QUIT in response to a 554 error        //https://www.rfc-editor.org/rfc/rfc5321#section-3.1        if ($responseCode === 554) {            $this->quit();        }        //This will handle 421 responses which may not wait for a QUIT (e.g. if the server is being shut down)        $this->edebug('Connection: closing due to error', self::DEBUG_CONNECTION);        $this->close();        return false;    }

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.