SMTP_Validate_Email — Email verification via SMTP written in PHP +

Working on a recent project I noticed that about 20% of user’s accounts have not been fully activated (typos in email addresses and/or bounced activation emails being the main culprits). After trying out several solutions (and realizing none of them worked for me), here’s my solution (pasting and excerpt from the README):

SMTP_Validate_Email – Perform email address verification via SMTP.

The class retrieves MX records for the email domain and then connects
to the domain’s SMTP server to try figuring out if the address is really valid.

Some features (check the source for more):

  • Not really sending a message, gracefully resetting the session when done
  • Command-specific communication timeouts implemented per the relevant RFCs
  • Catch-all account detection
  • Batch mode processing supported
  • MX query support on Windows without requiring any PEAR packages
  • Logging and debugging support

The source is over on Github:

12 Responses to “SMTP_Validate_Email — Email verification via SMTP written in PHP”

  1. Should be very useful. Thank you for writing this article

  2. Tried your email validator.

    For some reason, it cannot connect to hosts which I know are valid using a valid email.

    Any suggestions


  3. Hi Joe, it could be many things, but the usual culprit is a firewall. It could be running on the host you’re trying to run the script on, or on the receiving host, or (most probably) both. It could also be that some of the native php functions used in the script are disabled on your host.

    First thing you should try is setting $debug = true on line 80, and see if you get any useful output / error messages.

    If that doesn’t help, try removing the ‘@’ on line 353 before the stream_socket_client() call and see if that gets you anywhere…


  4. Hi Tomas
    With debug on and no changes I get:

    Connecting to
    Connect failed: Connection timed out, error number: 110, host:
    Unable to connect. Exception caught: Cannot open a connection to remote host (

    Connecting to
    Connect failed: Connection timed out, error number: 110, host:
    Unable to connect. Exception caught: Cannot open a connection to remote host (

    I then remove:
    the ‘@’ on line 353 before the stream_socket_client()

    I still get the same errors

    Any other suggestions


  5. Ok, it’s timing out.

    The remote host could be allowing only whitelisted connections (in which case theres not much you can do about it except mabe spoof your host’s IP address, and thats a long shot), or even silently dropping all connections to port 25 (because it only wants clients to talk via TLS or such, in which case you can try connecting to a different port and implement the missing TLS handling code). Or it could be just that the remote host is slow.

    Try increasing $connect_timeout and/or changing the $connect_port variables – or try running the script on another host (the current one could be blacklisted in sbl/dnsbl or such). I’m affraid you’ll need a bit deeper understanding of the underlying protocols and PHP to continue down the TLS road…

  6. Hi Thomas

    I found some interesting results but I guess it was too much information for your comment section to handle.

    I sent you an email which I hope you get.

    Please give me your comments


  7. Hi Tomas

    I sent you another email

    Any comments


  8. Hi Tomas

    Got your email

    Tried your script but could not connect to any mail server.

    I guess I will not be able to truly validate emails

    Is being on shared hosting part of the problem


  9. Hi Tomas

    I put your script on one of my other websites.
    If I tested emails associated with the website, your script did tell me if they were valid or not.

    However, when I tried to test emails not associated with my website, it would not connect.


    Too bad I can’t get it to work on my primary website.

  10. Joe, I get the same results as you. I’m also using a shared hosting plan. If I check email accounts that are on my server, it comes back fine. If I try some other yahoo or gmail account, it fails to connect.

  11. Do you have this script available on a webpage so we can do a test run?

  12. @Tom Blue: I used to have an example here , but it got abused a lot (as expected). And I think it could be running an outdated version. So YMMV.

    Results depend on *a lot* of factors, and querying from a publicly accessible form is not how you should really be using this. More info and details at the link above + this github issue: