October 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Validating Emails with PHP

  • December 17, 2003
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Validating Domain Existence

Once the email's syntax is validated, it's time to ensure that the domain exists and is configured to accept mail. The easiest way to do so is to verify that an MX (Mail Exchange) record for that domain exists. You can do so easily using PHP's getmxrr() function, introduced here.

getmxrr()

int getmxrr(string hostname array mxrr [, array weight])

The getmxrr() function will contact a DNS server in an attempt to determine whether MX (mail exchange) records for that host exist, returning TRUE if the records are found, and FALSE otherwise. If records are found, they are placed within the input parameter mxrr. If the optional weight array parameter is included, then the respective weight attributes for each record are placed there. However, because we're only interested in determining whether MX records for the given domain exist, we can invoke getmxrr() like so:

<?php
   $email = "johnny.rocket@example.com";
   list($username,$domain) = split("@",$email);
   if (getmxrr($domain,$mxrecords))
      echo "Email domain exists!";
   else
      echo "Email domain does not exist!";
?>

Putting it All Together

Let's assemble both the syntax and existence validation logic. To encourage reuse, the logic is incorporated into a function named validate_email(), which accepts as input a single parameter, $email. The function, along with a usage example is presented here:

<?php

function validate_email($email)
{

   // Create the syntactical validation regular expression
   $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";

   // Presume that the email is invalid
   $valid = 0;

   // Validate the syntax
   if (eregi($regexp, $email))
   {
      list($username,$domaintld) = split("@",$email);
      // Validate the domain
      if (getmxrr($domaintld,$mxrecords))
         $valid = 1;
   } else {
      $valid = 0;
   }

   return $valid;

}

$email = "johnny-rocket@example.com";

if (validate_email($email))
   echo "Email is valid!";
else
   echo "Email is invalid!";

?>

Conclusion

Given the mission-critical importance of communicating order confirmations, download instructions, newsletters and the like via email, taking the steps necessary to validate such user-input items will be well worth your effort. As you've learned in this tutorial, the ease in which such a feature can be implemented really leaves you with no real excuse for not doing so! If you wind up adding email validation to your application as a result of this tutorial, I'd love to hear about it! Please email me at jason@wjgilmore.com with all the details.

About the Author

W. Jason Gilmore (http://www.wjgilmore.com/ ) is an Internet application developer for the Fisher College of Business. He's the author of the upcoming book, PHP 5 and MySQL: Novice to Pro, due out by Apress in 2004. His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed, Zend.com, and Webreview. Jason is also the author of A Programmer's Introduction to PHP 4.0 (453pp., Apress). Along with colleague Jon Shoberg, he's co-author of "Out in the Open," a monthly column published within Linux magazine.

# # #





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel