Search code examples
javaphp.netrubyemail-bounces

validate email ids for hard bounce


I have tried searching but I got only online paid services, no code to test that given email address is really exists, so I can prevent a hard bounce.

Please see the below code which can only check the domain. Need help is it possible. Solution can be in any language, PHP (preferred), JAVA, .NET, Rudy

public function check_domain($email)
{
    if( ! (bool) checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX'))
    {
        return FALSE;
    }

    return TRUE;
}

Update

Using online services it shows something like this

MX record found: mta6.am0.yahoodns.net (Priority 1)
MX record found: mta5.am0.yahoodns.net (Priority 1)
MX record found: mta7.am0.yahoodns.net (Priority 1)
Connecting to mta6.am0.yahoodns.net
Connected to mta6.am0.yahoodns.net
Dialog with mta6.am0.yahoodns.net ok
------------------------------------------------------------
220 mta1278.mail.bf1.yahoo.com ESMTP ready
HELO verifyemailaddress.org
250 mta1278.mail.bf1.yahoo.com
MAIL FROM: <noreply@verifyemailaddress.org>
250 sender <noreply@verifyemailaddress.org> ok
RCPT TO: <xxxxx@yahoo.com>
250 recipient <xxxxxx@yahoo.com> ok
QUIT
221 mta1278.mail.bf1.yahoo.com
------------------------------------------------------------
Email address xxxxxx@yahoo.com accepted

What they are doing exactly, sending an email ??


Solution

  • At the very least, an SMTP connection needs to be opened to the receiving server in order to check that an email address exists. An email does not actually need to be sent. Most email servers will respond to the RCPT TO command with an error if the email does not exist. At this point, the QUIT command can be sent without actually sending an email. Some email servers will also let you issues the VRFY command to test an address, but this is not widely supported.

    This is how email verification services work. They perform these handshakes and then store the results. That's what you see happening in the example you provided; the client is QUITting after RCPT TO.

    There are a few other options:

    1. Most basic - test that the email address has valid structure. This page has one regular expression to check that - http://www.regular-expressions.info/email.html

    2. More advanced - depends on for what purpose you are trying to prevent hard bounces. I once toyed with the idea of putting heuristics where e.g. user name should be min X characters long, and domain Y char long, but abandoned it. Single character user names are valid; I am not sure about the minimum for domain name. And if someone wants to beat those rules it is trivial; so no point.

    3. More advanced - when you encounter a new domain, connect to the receiving SMTP server and begin an SMTP handshake. Check the Response to the RCPT TO command.

    4. Use an email verification service such as Kickbox. These services see many many email addresses and often have the relevant data cached and ready to go without waiting or having to perform a handshake.

    5. Why not simply send an email and ask users to click on a link. That is what many services have started doing. Weeds out hard bounces. Plus it ensures that the subscribers are providing an email address which they own.