function valid_email_address

You are here

7 common.inc valid_email_address($mail)
4.6 common.inc valid_email_address($mail)
4.7 common.inc valid_email_address($mail)
5 common.inc valid_email_address($mail)
6 common.inc valid_email_address($mail)
8 common.inc valid_email_address($mail)

Verifies the syntax of the given e-mail address.

This uses the PHP e-mail validation filter.

Parameters

$mail: A string containing an e-mail address.

Return value

TRUE if the address is in a valid format.

Related topics

8 calls to valid_email_address()
comment_form_validate in modules/comment/comment.module
Validate comment form submissions.
contact_category_edit_form_validate in modules/contact/contact.admin.inc
Form validation handler for contact_category_edit_form().
contact_personal_form_validate in modules/contact/contact.pages.inc
Form validation handler for contact_personal_form().
contact_site_form_validate in modules/contact/contact.pages.inc
Form validation handler for contact_site_form().
system_send_email_action_validate in modules/system/system.module
Validate system_send_email_action form submissions.

... See full list

File

includes/common.inc, line 1186
Common functions that many Drupal modules will need to reference.

Code

function valid_email_address($mail) {
  return (bool) filter_var($mail, FILTER_VALIDATE_EMAIL);
}

Comments

As is mentioned on the Drupal 6 API docs page for this function, an email address in the form of username@domain (without a TLD, like .com, .org, or .net...) will be accepted as valid—on certain PHP installations...

I found that on my MAMP default install (PHP 5.2.17), the non-TLD email addresses returned a FALSE, while on one of my production servers (PHP 5.2.13), non-TLD addresses returned TRUE.

According to comments on the PHP docs about filters, this condition is only true beginning with PHP 5.3. There is also an issue in the Drupal queue wherein catch mentions upgrading to PHP 5.2.9+, so this may exist as far back as 5.2.9. Based on your testing, there may also be a configuration option in place, since one would expect it to return TRUE when running on either 5.2.13 or 5.2.17.

So:

  • PHP 5.2 (potentially only up to 5.2.8): valid_email_address('john@example') returns FALSE
  • PHP 5.3: valid_email_address('john@example') returns TRUE

This is not a bug, technically, but should be taken into account when making use of this function.

not the dot at the end of "test." - this is a bug IMHO - this is a valid email address - I know 2 people that have an address with a dot at the end of ther $user part.

I ran this address through an online validator and it rejected it based on RFC 5322 (which claims to obsolete RFC 2822, and presumably is what FILTER_VALIDATE_EMAIL runs off). According to this RFC, "Neither part of the address may end with a dot".

Even if it wasn't, though, this would be something to raise to PHP rather than Drupal...