We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

tylerhall on 07/26/06


Tagged

regex email valid textmate test mx


Versions (?)


Who likes this?

22 people have marked this snippet as a favorite

jonhenshaw
runebrand
Fixe
dmclark
Krullies
pckujawa
NexusRex
mdavie
blakeb
demods
vali29
hudge
marteki
Nix
romanos
heinz1959
pixelhandler
mrjthethird
blackabee
shii
AlastairDewar
sumandahal


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 

You need to login to post a comment.