Implementing Email Validation Using PHP

Here's how to implement email address validation on your site using a very simple PHP script.

PHP email validation

PHP is the most widely used server side scripting language for web sites, applications and content management systems. As such, I've created a fully working example email validation script for PHP. It's free to use, modify as you see fit.

Warnings, Disclaimers and Caveats

The script provided here is for education purposes only and is provided "as-is".

It has many known short-comings in non-functional and functional aspects.

  • It doesn't perform or scale well - don't expect to be doing millions of email verifications with this script
  • Limited coverage - does not work very well with some mail services (e.g. Hotmail, Yahoo)
  • Intermittent connectivity - your servers IP address can be temporarily or permanently banned with excessive use of this script. When IP blocks happen, the script stops working
  • Incorrect results - results can come back as "Ok" when infact the actual answer is "Bad" (confirmed by sending email to address). This is because script has no way of identifying "catch all" domains.

Please don't expect industrial grade results from this script. It is simple, excludes a lot of the fine tuning code (omitted for clarity) needed to deliver robust, accurate email verification systems.

Email Verification - The Process Defined

Before deep diving into the code itself, let's go through the sequence of processing (with some code snippets) used in typical server side email verification.

Step 1 - Syntax Validation

Check the email for valid syntax. For example, is clearly not an email address whereas [email protected] could be.

We can check for basic syntax compliance by doing pattern recognition checks (perhaps using Regular Expressions (regex)) with the example below:

Example of Syntax Validation of Email Address Using Regular Expressions

        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", "[email protected]"))
            echo "bad syntax";

Step 2 - DNS Checks

Extract the "domain" from the email address. As in, the domain for [email protected] is

Example Of How To Extract Domain From Email Address

        // load the user and domain name into a local list from email address using string split function.
        list ( $Username, $Domain ) = split ("@","[email protected]");

There are a couple of records in DNS that we are interested in for our purposes:

  1. The A record.
  2. The MX record.

Technically, in most (but not all) situations a valid email address will have both A record(s) and MX record(s).

For simplicity, the demonstration script just looks for at least one valid MX record.

Example Of How To Get MX Record Using PHP

        // check if domain has MX record(s)
        if ( checkdnsrr ( "", "MX" ) )
            //MX record(s) present for domain, do more processing
            echo "Yay! Possibly a valid email address. Let's do more to find out.";
            /*No MX record(s) present for domain. This means no mail servers defined for domain. 
            Unlikely, therefore, that email address is valid.*/
            echo "Boo! Bailing out to report invalid email address.";

Step 3 - Mailbox Validation

Once DNS checks are complete, we pick a MX record for the domain and establish a TCP connection on port 25 (default port for SMTP).

Example Of How To Connect To a Mail Server Using PHP

        // Get DNS MX records from domain
        getmxrr ("", $MXHost));
        // Get the first MX record IP address
        $ConnectAddress = $MXHost[0];
        // Open TCP connection to IP address on port 25 (default SMTP port)
        $Connect = fsockopen ( $ConnectAddress, 25 );

Once connected to the remote mail server, the next bit of the process consists of our script having a "chat" with the remote mail server according to a pre-defined application level chat protocol known as SMTP.

SMTP is well documented elsewhere so I'm not going to repeat the entire conversation here. For mailbox verification we go as far as the RCPT TO:<[email protected]> step in the protocol. The response code (a number) returned from RCPT TO: is usually enough to give a good enough indication as to whether a mail address is valid or not.

Example Of RCPT TO Email Validation

        //... stuff that came before
        // Next, do RCPT TO:
        fputs ( $Connect, "RCPT TO: <>\r\n" );
        $to_reply = fgets ( $Connect, 1024 );
        // Quit the SMTP conversation and disconnect from remote mail server.
        fputs ( $Connect, "QUIT\r\n");

Complete Server Side PHP For Email Validation

Please see Github for the complete PHP email validation script.

Next Steps

Click here to get started with our industry leading valid email validation service.


Rowland O'Connor
Author Profile:
Rowland has previously had careers as an electical engineer, project manager, IT consultant, affiliate marketer, software and infrastructure architect and SCUBA diving instructor. Currenty roles are: dad, husband. Works as CEO at Email Hippo Ltd.