is_email

Verifies that an email is valid.

Description

(string|bool) is_email( (string) $email, (bool) $check_dns = false ); 

Does not grok i18n domains. Not RFC compliant.

Returns (string|bool)

Either false or the valid email address.

Parameters (2)

0. $email (string)
Email address to verify.
1. $check_dns — Optional. (bool) => false
Whether to check the DNS for the domain using checkdnsrr().

Usage

  1. if ( !function_exists( 'is_email' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/backpress/functions.formatting.php'; 
  3.  
  4. // Email address to verify. 
  5. $email = ''; 
  6.  
  7. // Whether to check the DNS for the domain using checkdnsrr(). 
  8. $check_dns = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = is_email($email, $check_dns); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/backpress/functions.formatting.php  
  1. function is_email( $email, $check_dns = false ) { 
  2. // Test for the minimum length the email can be 
  3. if ( strlen( $email ) < 3 ) { 
  4. return apply_filters( 'is_email', false, $email, 'email_too_short' ); 
  5.  
  6. // Test for an @ character after the first position 
  7. if ( strpos( $email, '@', 1 ) === false ) { 
  8. return apply_filters( 'is_email', false, $email, 'email_no_at' ); 
  9.  
  10. // Split out the local and domain parts 
  11. list( $local, $domain ) = explode( '@', $email, 2 ); 
  12.  
  13. // LOCAL PART 
  14. // Test for invalid characters 
  15. if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) { 
  16. return apply_filters( 'is_email', false, $email, 'local_invalid_chars' ); 
  17.  
  18. // DOMAIN PART 
  19. // Test for sequences of periods 
  20. if ( preg_match( '/\.{2, }/', $domain ) ) { 
  21. return apply_filters( 'is_email', false, $email, 'domain_period_sequence' ); 
  22.  
  23. // Test for leading and trailing periods and whitespace 
  24. if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) { 
  25. return apply_filters( 'is_email', false, $email, 'domain_period_limits' ); 
  26.  
  27. // Split the domain into subs 
  28. $subs = explode( '.', $domain ); 
  29.  
  30. // Assume the domain will have at least two subs 
  31. if ( 2 > count( $subs ) ) { 
  32. return apply_filters( 'is_email', false, $email, 'domain_no_periods' ); 
  33.  
  34. // Loop through each sub 
  35. foreach ( $subs as $sub ) { 
  36. // Test for leading and trailing hyphens and whitespace 
  37. if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) { 
  38. return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' ); 
  39.  
  40. // Test for invalid characters 
  41. if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) { 
  42. return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' ); 
  43.  
  44. // DNS 
  45. // Check the domain has a valid MX and A resource record 
  46. if ( $check_dns && function_exists( 'checkdnsrr' ) && !( checkdnsrr( $domain . '.', 'MX' ) || checkdnsrr( $domain . '.', 'A' ) ) ) { 
  47. return apply_filters( 'is_email', false, $email, 'dns_no_rr' ); 
  48.  
  49. // Congratulations your email made it! 
  50. return apply_filters( 'is_email', $email, $email, null );