wppaste
WordPress

SMTP::authenticate( string $username, string $password, string $authtype = null, PHPMailer\PHPMailer\OAuthTokenProvider $OAuth = null ): bool

Source
wp-includes/PHPMailer/SMTP.php:531
Perform SMTP authentication.

Description

Must be run after hello().

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

$usernamestring
The user name
$passwordstring
The password
$authtypestringoptional
The auth type (CRAM-MD5, PLAIN, LOGIN, XOAUTH2)Default: null
$OAuthPHPMailer\PHPMailer\OAuthTokenProvideroptional
An optional OAuthTokenProvider instance for XOAUTH2 authenticationDefault: null

Return value

bool
True if successfully authenticated

Performance profile

How much work a call to SMTP::authenticate() 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
10–111

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

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

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

WhenInstructionsCalls it makes
always10–29->setError()
$OAuth === null15–24none
!->sendCommand()19–26->sendCommand()
$OAuth !== null27–40->getOauth64(), ->sendCommand()
->sendCommand()28–34->sendCommand(), base64_encode(), ->sendCommand()
$OAuth !== null && $oauth !== "" && ->sendCommand()36–50->getOauth64(), ->sendCommand(), ->sendCommand()
->sendCommand()37–42->sendCommand(), base64_encode(), ->sendCommand(), base64_encode(), ->sendCommand()
always40–60->edebug(), ->edebug(), ->setError()
->sendCommand()40–47->sendCommand(), base64_decode(), ->hmac(), base64_encode(), ->sendCommand()
!empty($authtype) && $authtype && $OAuth === null41–55->edebug(), ->edebug()
!empty($authtype) && $authtype && !->sendCommand()45–57->edebug(), ->edebug(), ->sendCommand()
$OAuth !== null && $oauth !== "" && ->sendCommand()47–56->getOauth64(), ->sendCommand(), ->sendCommand(), ->sendCommand()
24 further outcomes, up to 111 instructions
!empty($authtype) && $authtype && $OAuth === null51–72->edebug(), ->edebug(), ->edebug()
always52–77->edebug(), ->edebug(), ->edebug(), ->setError()
!empty($authtype) && $authtype && $OAuth !== null53–71->edebug(), ->edebug(), ->getOauth64(), ->sendCommand()
!empty($authtype) && $authtype && ->sendCommand()54–65->edebug(), ->edebug(), ->sendCommand(), base64_encode(), ->sendCommand()
!empty($authtype) && $authtype && !->sendCommand()55–74->edebug(), ->edebug(), ->edebug(), ->sendCommand()
!empty($authtype) && $authtype && $OAuth !== null && $oauth !== "" && ->sendCommand()62–81->edebug(), ->edebug(), ->getOauth64(), ->sendCommand(), ->sendCommand()
$authtype !== null && $OAuth === null63–79->edebug(), ->edebug(), ->edebug(), ->edebug()
!empty($authtype) && $authtype && $OAuth !== null63–88->edebug(), ->edebug(), ->edebug(), ->getOauth64(), ->sendCommand()
!empty($authtype) && $authtype && ->sendCommand()63–73->edebug(), ->edebug(), ->sendCommand(), base64_encode(), ->sendCommand(), base64_encode(), ->sendCommand()
!empty($authtype) && $authtype && ->sendCommand()64–82->edebug(), ->edebug(), ->edebug(), ->sendCommand(), base64_encode(), ->sendCommand()
$authtype !== null && !$authtype66–84->edebug(), ->edebug(), ->edebug(), ->edebug(), ->setError()
!empty($authtype) && $authtype && ->sendCommand()66–78->edebug(), ->edebug(), ->sendCommand(), base64_decode(), ->hmac(), base64_encode(), ->sendCommand()
$authtype !== null && !->sendCommand()67–81->edebug(), ->edebug(), ->edebug(), ->edebug(), ->sendCommand()
!empty($authtype) && $authtype && $OAuth !== null && $oauth !== "" && ->sendCommand()72–98->edebug(), ->edebug(), ->edebug(), ->getOauth64(), ->sendCommand(), ->sendCommand()
!empty($authtype) && $authtype && $OAuth !== null && $oauth !== "" && ->sendCommand()73–87->edebug(), ->edebug(), ->getOauth64(), ->sendCommand(), ->sendCommand(), ->sendCommand()
!empty($authtype) && $authtype && ->sendCommand()73–90->edebug(), ->edebug(), ->edebug(), ->sendCommand(), base64_encode(), ->sendCommand(), base64_encode(), ->sendCommand()
$authtype !== null && $OAuth !== null75–95->edebug(), ->edebug(), ->edebug(), ->edebug(), ->getOauth64(), ->sendCommand()
$authtype !== null && ->sendCommand()76–89->edebug(), ->edebug(), ->edebug(), ->edebug(), ->sendCommand(), base64_encode(), ->sendCommand()
!empty($authtype) && $authtype && ->sendCommand()76–95->edebug(), ->edebug(), ->edebug(), ->sendCommand(), base64_decode(), ->hmac(), base64_encode(), ->sendCommand()
!empty($authtype) && $authtype && $OAuth !== null && $oauth !== "" && ->sendCommand()83–104->edebug(), ->edebug(), ->edebug(), ->getOauth64(), ->sendCommand(), ->sendCommand(), ->sendCommand()
$authtype !== null && $OAuth !== null && $oauth !== "" && ->sendCommand()84–105->edebug(), ->edebug(), ->edebug(), ->edebug(), ->getOauth64(), ->sendCommand(), ->sendCommand()
$authtype !== null && ->sendCommand()85–97->edebug(), ->edebug(), ->edebug(), ->edebug(), ->sendCommand(), base64_encode(), ->sendCommand(), base64_encode(), ->sendCommand()
$authtype !== null && ->sendCommand()88–102->edebug(), ->edebug(), ->edebug(), ->edebug(), ->sendCommand(), base64_decode(), ->hmac(), base64_encode(), ->sendCommand()
$authtype !== null && $OAuth !== null && $oauth !== "" && ->sendCommand()95–111->edebug(), ->edebug(), ->edebug(), ->edebug(), ->getOauth64(), ->sendCommand(), ->sendCommand(), ->sendCommand()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev24310–11133
8.524310–11133
8.424310–1113318 fewer instructions than PHP 8.3
8.326110–12633
8.226110–126331 more instruction than PHP 8.1
8.126010–12633
7.426010–12633

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

  • \PHPMailer\PHPMailer\SMTP::setError()
  • \PHPMailer\PHPMailer\SMTP::edebug()
  • \PHPMailer\PHPMailer\SMTP::sendCommand()
  • \PHPMailer\PHPMailer\SMTP::hmac()
  • \PHPMailer\PHPMailer\Exception::__construct()

Source code

    public function authenticate(        $username,        $password,        $authtype = null,        $OAuth = null    ) {        if (!$this->server_caps) {            $this->setError('Authentication is not allowed before HELO/EHLO');             return false;        }         if (array_key_exists('EHLO', $this->server_caps)) {            //SMTP extensions are available; try to find a proper authentication method            if (!array_key_exists('AUTH', $this->server_caps)) {                $this->setError('Authentication is not allowed at this stage');                //'at this stage' means that auth may be allowed after the stage changes                //e.g. after STARTTLS                 return false;            }             $this->edebug('Auth method requested: ' . ($authtype ?: 'UNSPECIFIED'), self::DEBUG_LOWLEVEL);            $this->edebug(                'Auth methods available on the server: ' . implode(',', $this->server_caps['AUTH']),                self::DEBUG_LOWLEVEL            );             //If we have requested a specific auth type, check the server supports it before trying others            if (null !== $authtype && !in_array($authtype, $this->server_caps['AUTH'], true)) {                $this->edebug('Requested auth method not available: ' . $authtype, self::DEBUG_LOWLEVEL);                $authtype = null;            }             if (empty($authtype)) {                //If no auth mechanism is specified, attempt to use these, in this order                //Try CRAM-MD5 first as it's more secure than the others                foreach (['CRAM-MD5', 'LOGIN', 'PLAIN', 'XOAUTH2'] as $method) {                    if (in_array($method, $this->server_caps['AUTH'], true)) {                        $authtype = $method;                        break;                    }                }                if (empty($authtype)) {                    $this->setError('No supported authentication methods found');                     return false;                }                $this->edebug('Auth method selected: ' . $authtype, self::DEBUG_LOWLEVEL);            }             if (!in_array($authtype, $this->server_caps['AUTH'], true)) {                $this->setError("The requested authentication method \"$authtype\" is not supported by the server");                 return false;            }        } elseif (empty($authtype)) {            $authtype = 'LOGIN';        }        switch ($authtype) {            case 'PLAIN':                //Start authentication                if (!$this->sendCommand('AUTH', 'AUTH PLAIN', 334)) {                    return false;                }                //Send encoded username and password                if (                    //Format from https://www.rfc-editor.org/rfc/rfc4616#section-2                    //We skip the first field (it's forgery), so the string starts with a null byte                    !$this->sendCommand(                        'User & Password',                        base64_encode("\0" . $username . "\0" . $password),                        235                    )                ) {                    return false;                }                break;            case 'LOGIN':                //Start authentication

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.