6

I have a question, i have a php script to check if the email address exist.

But appear that yahoo, hotmail, aol and others providers are accepting any emails and not rejecting the invalid emails.

Only Gmail, and many domains like stackoverflow.com are rejecting the no vaild emails.

Check my script and let me know if i can do some to check the yahoo and others.

html post form

<html>
<body>
<form action="checkemail.php" method="POST">
<b>E-mail</b> <input type="text" name="email">
<input type="submit">
</form>
</body>
</html>

php

<?php

/* Validate an email address. */
function jValidateEmailUsingSMTP($sToEmail, $sFromDomain = "gmail.com", $sFromEmail = "[email protected]", $bIsDebug = false) {

    $bIsValid = true; // assume the address is valid by default..
    $aEmailParts = explode("@", $sToEmail); // extract the user/domain..
    getmxrr($aEmailParts[1], $aMatches); // get the mx records..

    if (sizeof($aMatches) == 0) {
        return false; // no mx records..
    }

    foreach ($aMatches as $oValue) {

        if ($bIsValid && !isset($sResponseCode)) {

            // open the connection..
            $oConnection = @fsockopen($oValue, 25, $errno, $errstr, 30);
            $oResponse = @fgets($oConnection);

            if (!$oConnection) {

                $aConnectionLog['Connection'] = "ERROR";
                $aConnectionLog['ConnectionResponse'] = $errstr;
                $bIsValid = false; // unable to connect..

            } else {

                $aConnectionLog['Connection'] = "SUCCESS";
                $aConnectionLog['ConnectionResponse'] = $errstr;
                $bIsValid = true; // so far so good..

            }

            if (!$bIsValid) {

                if ($bIsDebug) print_r($aConnectionLog);
                return false;

            }

            // say hello to the server..
            fputs($oConnection, "HELO $sFromDomain\r\n");
            $oResponse = fgets($oConnection);
            $aConnectionLog['HELO'] = $oResponse;

            // send the email from..
            fputs($oConnection, "MAIL FROM: <$sFromEmail>\r\n");
            $oResponse = fgets($oConnection);
            $aConnectionLog['MailFromResponse'] = $oResponse;

            // send the email to..
            fputs($oConnection, "RCPT TO: <$sToEmail>\r\n");
            $oResponse = fgets($oConnection);
            $aConnectionLog['MailToResponse'] = $oResponse;

            // get the response code..
            $sResponseCode = substr($aConnectionLog['MailToResponse'], 0, 3);
            $sBaseResponseCode = substr($sResponseCode, 0, 1);

            // say goodbye..
            fputs($oConnection,"QUIT\r\n");
            $oResponse = fgets($oConnection);

            // get the quit code and response..
            $aConnectionLog['QuitResponse'] = $oResponse;
            $aConnectionLog['QuitCode'] = substr($oResponse, 0, 3);

            if ($sBaseResponseCode == "5") {
                $bIsValid = false; // the address is not valid..
            }

            // close the connection..
            @fclose($oConnection);

        }

    }

    if ($bIsDebug) {
        print_r($aConnectionLog); // output debug info..
    }

    return $bIsValid;

}
$email = $_POST['email'];

$bIsEmailValid = jValidateEmailUsingSMTP("$email", "gmail.com", "[email protected]");
echo $bIsEmailValid ? "<b>Valid!</b>" : "Invalid! :(";
?>
8
  • 4
    This is not very reliable way to verify email addresses. Many server wont properly return code indicating if an email exists or not to prevent spam harvesting.
    – datasage
    Jan 21, 2013 at 22:54
  • 4
    I think that many providers will not let you test out email addresses like this (because this same technique can be used by someone to mine valid emails for spam). The only real way to verify an email address is to send a an email with a unique token and have a mechanism that allows the user to enter the token into your application to confirm that they received the email.
    – larsks
    Jan 21, 2013 at 22:54
  • 4
    this will never work with any degree of reliability
    – user557846
    Jan 21, 2013 at 22:58
  • 2
    The typical flow is to send a verification email to the address and have to user perform the verification either via pressing a link inside the email or enter some code; replying to the email is yet another option.
    – Ja͢ck
    Jan 21, 2013 at 23:02
  • 2
    well you cant. end of story :(
    – user557846
    Jan 21, 2013 at 23:08

4 Answers 4

13

If you need to make super sure that an E-Mail address exists, send an E-Mail to it. It should contain a link with a random ID. Only when that link is clicked, and contains the correct random ID, the user's account is activated (or ad published, or order sent, or whatever it is that you are doing).

This is the only reliable way to verify the existence of an E-Mail address, and to make sure that the person filling in the form has access to it.

2
  • 5
    but of course we all use those disposable addresses, so its only 'valid' for a very short period of time
    – user557846
    Jan 21, 2013 at 23:04
  • 1
    @Dagon, addresses from services such as mailinator.com are always valid. They may not be monitored all the time, but they always accept and show incoming email. Dec 18, 2014 at 23:21
11

There is no 100% reliable way of checking the validity of an email address. There are a few things you can do to at least weed out obviously invalid addresses, though.

The problems that arise with email addresses is actually very similar to those of snail mail. All three points below can also be used for sending snail mail (just change the DNS record with a physical address).

1. Check that the address is formatted correctly

It is very difficult to check the format of email addresses, but PHP has a validation filter that attempts to do it. The filter does not handle "comments and folding whitespace", but I doubt anyone will notice.

if (filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE) {
    echo 'Valid email address formatting!';
}

2. Check that the DNS record exists for the domain name

If a DNS (Domain Name System) record exists then at least someone has set it up. It does not mean that there is an email server at the address, but if the address exists then it is more likely.

$domain = substr($email, strpos($email, '@') + 1);
if  (checkdnsrr($domain) !== FALSE) {
    echo 'Domain is valid!';
}

3. Send a verification email to the address

This is the most effective way of seeing if someone is at the other end of the email address. If a confirmation email is not responded to in an orderly fashion -- 3 hours for example -- then there is probably some problem with the address.

4
  • try the script please. yahoo is getting valid for [email protected] so for yahoo user i cannot check. Jan 21, 2013 at 23:08
  • ok, but here i can check if the domain exist, is easy to check in browswer this, and to check if is good formated is not reliable for me. Thanks Jan 21, 2013 at 23:15
  • You need to modify your code. As you've put it at the moment $domain will return '@domain.com', which will cause the check to fail. $domain = substr($email, strpos($email, '@') + 1); works fine though. Oct 13, 2013 at 23:52
  • checkdnsrr is pretty neat :) Oct 9, 2017 at 23:51
7

You can validate "used or real" emails with Telnet and MX records more info in here, for PHP exists a great library call php-smtp-email-validation that simplify the process.

You create a boolean function with the file example1.php and call it when you'll validate the email text. For Gmail, Hotmail, Outlook, live and MSM I don's have any problems but with Yahoo and Terra the library can't validate correctly emails

1
  • 1
    I know the comment is old, but this won't work. It works only on a very limited set of providers as of 2013. No one should rely on it, or use it in a live environment.
    – Apache
    Aug 17, 2013 at 17:17
3

The problem is gmail uses port 25 for their smtp outgoing mails, the other providers use different ports for their connection. Your response seems okay for gmail.com.

When you connect to gmail smtp it gives you response 250 2.1.5 OK j5si2542844pbs.271 - gsmtp

But when you connect to any other smtp who does not use port 25 it gives you null response.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.