Check if the php email exists

I have a question, I have a php script to check if an email address exists.

But appear that yahoo, hotmail, aol and other providers accept any emails and do not reject invalid emails.

Only Gmail and many domains like stackoverflow.com reject emails no vaild.

Check my script and let me know if I can do some to check 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@gmail.com", $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@gmail.com");
echo $bIsEmailValid ? "<b>Valid!</b>" : "Invalid! :(";
?>

      

+3


source to share


4 answers


If you need to make sure that an email address exists, send it an E-Mail. It must contain a link with a random ID. Only when this link is clicked and contains the correct random ID is the user account activated (either a published post or a submitted order, or whatever you do).



This is the only reliable way to check for an email address and to ensure that the user filling out the form has access to it.

+12


source


There is no reliable way to 100% validate an email address. There are a few things you can do, at least to cut off obviously invalid addresses.

Problems with email addresses are actually very similar to problems with street mail. All three points below can also be used to send snail emails (just change the DNS record with the physical address).

1. Make sure the address is formatted correctly

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

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

      



2. Make sure a DNS record exists for the domain name

If DNS (Domain Name System) exists, then at least someone set it up. This does not mean that the address has a mail server, 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 confirmation email to

This is the most efficient way to see if someone is on the other end of an email address. If the confirmation email is not sent in an orderly manner - for example, 3 hours, then there are probably some address issues.

+10


source


You can check "used or real" emails with Telnet and MX for more details here , as there is an excellent php-smtp-email-validation library call for PHP that simplifies the process.

You create a boolean function with the file example1.php and call it when you validate the email body. For Gmail, Hotmail, Outlook, Live and MSM, I have any problems, but with Yahoo and Terra, the library cannot check email correctly

+5


source


The problem is that gmail uses a port 25

for their outgoing smtp messages, other providers use different ports for their connection. Your answer seems to fit 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 that doesn't use port 25 it gives you the answer null

.

+1


source







All Articles