| 5 common.inc | valid_email_address($mail) |
| 6 common.inc | valid_email_address($mail) |
| 7 common.inc | valid_email_address($mail) |
| 8 common.inc | valid_email_address($mail) |
Verifies the syntax of the given e-mail address.
See RFC 2822 for details.
Parameters
$mail: A string containing an e-mail address.
Return value
1 if the email address is valid, 0 if it is invalid or empty, and FALSE if there is an input error (such as passing in an array instead of a string).
Related topics
7 calls to valid_email_address()
File
- includes/
common.inc, line 1000 - Common functions that many Drupal modules will need to reference.
Code
function valid_email_address($mail) {
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
$domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+';
$ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
$ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
return preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail);
}
Login or register to post comments
Comments
Special cases
There a couple special cases which this algorithm doesn't cover, one is a series of email addresses separated by commas, another is "Name <"+email address+">". In these two cases the email addresses need to be parsed out.
I think that would be outside
I think that would be outside the scope of this function. You could easily pass addresses to this function to accomplish your specific needs.
$list = 'x@x.com,s@k,test@gmail.com,xx,fin@h.k.';foreach(explode(',', $list) as $email) {
echo $email, ( (valid_email_address($email)) ? ' good' : ' bad' ), '<br />';
}
Recipient Name required for Goldmine import
I agree with the first suggestion, that a recipient name should be allowed along with their email address.
This is a requirement for a client's website, where contact form messages are emailed to a specific address, which requires a specially-crafted recipient name for an automated process to work.
info@somedomain returns TRUE
This function returns TRUE, even for email addresses without the domain extension.
For example, info@somedomain returns TRUE which is actually a invalid email address.
The validation doesn't cover .com .net .org extensions!
couldn't info@somedomain be
couldn't info@somedomain be valid if drupal is deployed for use on an intranet?
neither does the RFC
info@somedomain is a valid email address from a syntax perspective. the fact that 'somedomain' cannot be a public domain name with a DNS entry notwithstanding :)
i agree that it would be nice for this function to go a little beyond the RFC
we need more complex stuff
we need more complex stuff here
check it http://code.google.com/p/php-smtp-email-validation/