Verify if email is from a valid domain in PHP

Working on an application which received user signups and let’s suppose it’s built on top of PHP, you want to validate that the email the user enters is valid. Sure, you’ll check that the email entered is a “syntactically” valid one by using one of these methods.

But, what if, let’s say, you also want to verify the email in question comes from a valid domain. i.e it’s not something non-existent such as [email protected] where doesn’t exist at all on the internet.

There’s a handy little function in PHP that let’s you do just that.

The checkdnsrr function

What you’d do is parse domain of the email using checkdnsrr like so.

if (checkdnsrr(array_pop(explode("@", "[email protected]com")), "MX")) {
     // valid email    

What happens here is, the checkdnsrr checks DNS records of given type (“MX” in this case) corresponding to a given email address. Here, “MX” is a mail exchanger record (MX record) specifies the mail server responsible for accepting email messages on behalf of a domain name. If “” exists in the above example, the funtion will return true.

You’d however need to check if the email is a valid “syntactically” before applying the above check for the domain existence because checkdnsrr will expect a fully qualified name in order to lookup for the MX records.

Source: Amit Merchant

Leave a Reply

Your email address will not be published.