wppaste
WordPress

PHPMailer::validateAddress( string $address, string|callable $patternselect = null ): bool

Source
wp-includes/PHPMailer/PHPMailer.php:1510
Check that a string looks like an email address.

Description

Validation patterns supported:

  • auto Pick best pattern automatically;
  • pcre8 Use the squiloople.com pattern, requires PCRE > 8.0;
  • pcre Use old PCRE implementation;
  • php Use PHP built-in FILTER_VALIDATE_EMAIL;
  • html5 Use the pattern given by the HTML5 spec for 'email' type form input elements.
  • eai Use a pattern similar to the HTML5 spec for 'email' and to firefox, extended to support EAI (RFC6530).
  • noregex Don't use a regex: super fast, really dumb.
    Alternatively you may pass in a callable to inject your own validator, for example:
PHPMailer::validateAddress('[email protected]', function($address) {
 return (strpos($address, '@') !== false);
});

You can also set the PHPMailer::$validator static to a callable, allowing built-in methods to use your validator.

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

$addressstring
The email address to check
$patternselectstring|callableoptional
Which pattern to useDefault: null

Return value

bool

Performance profile

How much work a call to PHPMailer::validateAddress() 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
12–34

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

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

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

WhenInstructionsCalls it makes
always12–29is_callable()
is_callable() && !is_string($patternselect)14–15is_callable(), call_user_func()
always21–34is_callable(), filter_var()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4812–3410
8.54812–34101 more instruction than PHP 8.4
8.44712–331015 fewer instructions than PHP 8.3
8.36214–3910
8.26214–3910
8.16214–3910
7.46214–3910

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.

Source code

    public static function validateAddress($address, $patternselect = null)    {        if (null === $patternselect) {            $patternselect = static::$validator;        }        //Don't allow strings as callables, see SECURITY.md and CVE-2021-3603        if (is_callable($patternselect) && !is_string($patternselect)) {            return call_user_func($patternselect, $address);        }        //Reject line breaks in addresses; it's valid RFC5322, but not RFC5321        if (strpos($address, "\n") !== false || strpos($address, "\r") !== false) {            return false;        }        switch ($patternselect) {            case 'pcre': //Kept for BC            case 'pcre8':                /*                 * A more complex and more permissive version of the RFC5322 regex on which FILTER_VALIDATE_EMAIL                 * is based.                 * In addition to the addresses allowed by filter_var, also permits:                 *  * dotless domains: `a@b`                 *  * comments: `1234 @ local(blah) .machine .example`                 *  * quoted elements: `'"test blah"@example.org'`                 *  * numeric TLDs: `[email protected]`                 *  * unbracketed IPv4 literals: `[email protected]`                 *  * IPv6 literals: 'first.last@[IPv6:a1::]'                 * Not all of these will necessarily work for sending!                 *                 * @copyright 2009-2010 Michael Rushton                 * Feel free to use and redistribute this code. But please keep this copyright notice.                 */                return (bool) preg_match(                    '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .                    '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .                    '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .                    '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .                    '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .                    '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .                    '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .                    '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .                    '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',                    $address                );            case 'html5':                /*                 * This is the pattern used in the HTML5 spec for validation of 'email' type form input elements.                 *                 * @see https://html.spec.whatwg.org/#e-mail-state-(type=email)                 */                return (bool) preg_match(                    '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}' .                    '[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD',                    $address                );            case 'eai':                /*                 * This is the pattern used in the HTML5 spec for validation of 'email' type                 * form input elements (as above), modified to accept Unicode email addresses.                 * This is also more lenient than Firefox' html5 spec, in order to make the regex faster.                 * 'eai' is an acronym for Email Address Internationalization.                 * This validator is selected automatically if you attempt to use recipient addresses                 * that contain Unicode characters in the local part.                 *                 * @see https://html.spec.whatwg.org/#e-mail-state-(type=email)                 * @see https://en.wikipedia.org/wiki/International_email                 */                return (bool) preg_match(                    '/^[-\p{L}\p{N}\p{M}.!#$%&\'*+\/=?^_`{|}~]+@[\p{L}\p{N}\p{M}](?:[\p{L}\p{N}\p{M}-]{0,61}' .                    '[\p{L}\p{N}\p{M}])?(?:\.[\p{L}\p{N}\p{M}]' .                    '(?:[-\p{L}\p{N}\p{M}]{0,61}[\p{L}\p{N}\p{M}])?)*$/usD',                    $address                );            case 'php':            default:                return filter_var($address, FILTER_VALIDATE_EMAIL) !== 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/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.