SMTP::connect( string $host, int $port = null, int $timeout = 30, array $options = [] ): bool
- Source
wp-includes/PHPMailer/SMTP.php:348
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
- Scaling
- Constant
- Instructions
- 14–81
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 88.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls 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 === 220 | 63–65 | ->setError(), ->connected(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug() |
!->connected() && $responseCode === 220 | 68–70 | ->setError(), ->connected(), var_export(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug() |
!->connected() && $responseCode !== 220 && $responseCode !== 554 | 72–74 | ->setError(), ->connected(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug(), ->edebug(), ->close() |
!->connected() && $responseCode !== 220 && $responseCode === 554 | 74–76 | ->setError(), ->connected(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug(), ->quit(), ->edebug(), ->close() |
!->connected() && $responseCode !== 220 && $responseCode !== 554 | 77–79 | ->setError(), ->connected(), var_export(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug(), ->edebug(), ->close() |
!->connected() && $responseCode !== 220 && $responseCode === 554 | 79–81 | ->setError(), ->connected(), var_export(), ->edebug(), ->getSMTPConnection(), ->edebug(), ->get_lines(), ->edebug(), ->quit(), ->edebug(), ->close() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 87 | 14–80 | 6 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 88 | 14–81 | 6 | |
| 8.4 | 88 | 14–81 | 6 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 91 | 14–84 | 6 | |
| 8.2 | 91 | 14–84 | 6 | |
| 8.1 | 91 | 14–84 | 6 | |
| 7.4 | 91 | 14–84 | 6 |
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.
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.