Posted By

mafhh14 on 03/13/13


Tagged

email php validator


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

Abiotic
simyaci
3polars


PHP Email Validator – Email MX DNS Record Check


 / Published in: PHP
 

URL: http://www.phpmoot.com/php-email-validator-%E2%80%93-email-mx-dns-record-check/

Validating an email address is one of the hardest feats on the web. A valid email can be marketing gold, but an invalid email address is dead weight. Not only does it require a CPU-taxing PHP regular expression ("/^[A-z0-9.-]+"."@" . "[A-z0-9][A-z0-9-]*". "(.[A-z0-9-]+)*".".([A-z]{2,6})$/"), the regular expression can be useless even after it's validated for format if the domain doesn't exist.

A regular expression simply wont do -- we need to think more low-level. What does email at any domain require? A DNS MX record. Well, PHP provides a great solution to validate that there's a MX record for the provided email address' domain.

  1. <?php
  2. function domain_exists($email, $record = 'MX'){
  3. list($user, $domain) = explode('@', $email);
  4. return checkdnsrr($domain, $record);
  5. }
  6. if(domain_exists('[email protected]')) {
  7. echo('This MX records exists; I will accept this email as valid.');
  8. }
  9. else {
  10. echo('No MX record exists; Invalid email.');
  11. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: simyaci on March 20, 2013

before explode can check multiple @ in email.. thanks.

You need to login to post a comment.