Skip to content

Commit

Permalink
Fix email validation regex rejects addresses with single subdomain
Browse files Browse the repository at this point in the history
Per RFC 5322, e.g. user@localhost is a valid e-mail address but the
previously implemented regex rejected it.

Fixes #14453
  • Loading branch information
dregad committed Jul 6, 2012
1 parent b88e409 commit abf661e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions core/email_api.php
Expand Up @@ -78,15 +78,24 @@
$g_phpMailer = null;

/**
*
* Use a simple perl regex for valid email addresses. This is not a complete regex,
* as it does not cover quoted addresses or domain literals, but it is simple and
* covers the vast majority of all email addresses without being overly complex.
* @return string
*/
function email_regex_simple() {
return "/([a-z0-9!#*+\/=?^_{|}~-]+(?:\.[a-z0-9!#*+\/=?^_{|}~-]+)*)" . # recipient
"\@((?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)/i"; # @domain
static $s_email_regex = null;

if( is_null( $s_email_regex ) ) {
$t_recipient = "([a-z0-9!#*+\/=?^_{|}~-]+(?:\.[a-z0-9!#*+\/=?^_{|}~-]+)*)";

# a domain is one or more subdomains
$t_subdomain = "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)";
$t_domain = "(${t_subdomain}(?:\.${t_subdomain})*)";

$s_email_regex = "/${t_recipient}\@${t_domain}/";
}
return $s_email_regex;
}

/**
Expand Down

0 comments on commit abf661e

Please sign in to comment.