Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if email exist php

Tags:

php

email

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! :(";
?>
like image 624
Neo Castelli Avatar asked Jan 21 '13 22:01

Neo Castelli


3 Answers

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.

like image 167
Pekka Avatar answered Nov 17 '22 06:11

Pekka


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.

like image 29
Sverri M. Olsen Avatar answered Nov 17 '22 06:11

Sverri M. Olsen


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

like image 7
Eduardo Chavira Avatar answered Nov 17 '22 05:11

Eduardo Chavira