ckeckMail


 / Published in: PHP
 

  1. function checkEmail($email)
  2. {
  3. // Create the syntactical validation regular expression
  4. $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
  5.  
  6. // Presume that the email is invalid
  7. $valid = 0;
  8.  
  9. // Validate the syntax
  10. if (eregi($regexp, $email))
  11. {
  12. list($username,$domaintld) = split("@",$email);
  13. // Validate the domain
  14. if (getmxrr($domaintld,$mxrecords))
  15. $valid = 1;
  16. } else {
  17. $valid = 0;
  18. }
  19.  
  20. return $valid;
  21.  
  22. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Nils on March 4, 2008

Nice. getmxrr - would this slow down the script if I use a non-existing domain?

Posted By: kapooht on December 26, 2010

@Nils, No. The function just checks characters within the string. It allows only lowercase letters (a through z), numbers (0 through 9), fullstops before checking if an @ symbol is somewhere in the middle and if a fullstop is somewhere after the @ symbol, as well as checking if there are no more 4, no less than 2 characters after that fullstop.

Posted By: wwwduvidasdeinformaticacom on February 10, 2011

@kapooht...Sorry but the getmxrr function should not be used for address verification.

"This function should not be used for the purposes of address verification. Only the mailexchangers found in DNS are returned, however, according to ยป RFC 2821 when no mail exchangers are listed, hostname itself should be used as the only mail exchanger with a priority of 0."

in http://php.net/manual/en/function.getmxrr.php

You need to login to post a comment.