Test for a valid email address and MX records


 / Published in: PHP
 

URL: http://snipplr.com/view/69/ckeckmail/

Adapted from http://snipplr.com/view/69/ckeckmail/

  1. // Tests for a valid email address and optionally tests for valid MX records, too.
  2. function is_valid_email($email, $test_mx = false)
  3. {
  4. if(eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email))
  5. if($test_mx)
  6. {
  7. list($username, $domain) = split("@", $email);
  8. return getmxrr($domain, $mxrecords);
  9. }
  10. else
  11. return true;
  12. else
  13. return false;
  14. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: bigker on November 22, 2009

--

Posted By: Calvista on September 16, 2010

Instead of :

return getmxrr($domain, $mxrecords);

This is better:

return (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'));

Posted By: buzzknow on September 17, 2010

nice calvista :)

You need to login to post a comment.