Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unhammer/f6752baa4a6fbfc78c79baff753d692d to your computer and use it in GitHub Desktop.
Save unhammer/f6752baa4a6fbfc78c79baff753d692d to your computer and use it in GitHub Desktop.
Test Wordpress email validation regular expression
<?php
/*
When using a current PHP version, class-phpmailer.php:validateAddress() uses a complex regex ("pcre8") for email address validation.
PHP < 7.3 uses libpcre 8.x.
PHP 7.3 uses libpcre2 10.x.
Due to a bug in libpcre2 < 10.32-RC1 https://bugs.exim.org/show_bug.cgi?id=2300,
this email regex validation fails in PHP 7.3 with PCRE_VERSION < 10.32.
One consequence of this is being unable to send a password reset link from the login page.
Submissions to /wp-login.php?action=lostpassword will show the error
"The email could not be sent. Possible reason: your host may have disabled the mail() function."
Sample outputs from this script:
=-=-=-=-=-=-=-=-=-=-=-=
PHP version: 7.2.18
PCRE version: 8.41 2017-07-05
1
Wordpress email validation preg_match succeeded
=-=-=-=-=-=-=-=-=-=-=-=
PHP version: 7.3.7
PCRE version: 10.30 2017-08-14
Wordpress email validation preg_match failed
=-=-=-=-=-=-=-=-=-=-=-=
PHP version: 7.3.7
PCRE version: 10.32 2018-09-10
1
Wordpress email validation preg_match succeeded
=-=-=-=-=-=-=-=-=-=-=-=
*/
$email_regex = '/^(?!(?>(?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';
echo "PHP version: " . phpversion() . "\n";
echo "PCRE version: " . PCRE_VERSION . "\n";
echo preg_match($email_regex, "example@wordpress.com") . "\n";
if (preg_match($email_regex, "example@wordpress.com")) {
echo "Wordpress email validation preg_match succeeded";
} else {
echo "Wordpress email validation preg_match failed";
}
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment