简体   繁体   English

检查电子邮件是否存在php

[英]Check if email exist php

I have a question, i have a php script to check if the email address exist. 我有一个问题,我有一个PHP脚本来检查电子邮件地址是否存在。

But appear that yahoo, hotmail, aol and others providers are accepting any emails and not rejecting the invalid emails. 但似乎yahoo,hotmail,aol和其他提供商正在接受任何电子邮件,而不是拒绝无效的电子邮件。

Only Gmail, and many domains like stackoverflow.com are rejecting the no vaild emails. 只有Gmail和许多域(例如stackoverflow.com)都拒绝没有无效的电子邮件。

Check my script and let me know if i can do some to check the yahoo and others. 检查我的脚本,让我知道我是否可以做一些检查yahoo和其他工具。

html post form html发布表格

<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

<?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! :(";
?>

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. 它应包含带有随机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). 仅当单击该链接并包含正确的随机ID时,该用户的帐户才会被激活(或发布广告,发送订单或执行任何操作)。

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. 这是验证电子邮件地址是否存在并确保填写表格的人可以访问它的唯一可靠方法。

There is no 100% reliable way of checking the validity of an email address. 没有100%可靠的方法来检查电子邮件地址的有效性。 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). 下面的所有三点也可以用于发送蜗牛邮件(只需使用物理地址更改DNS记录)。

1. Check that the address is formatted correctly 1.检查地址格式是否正确

It is very difficult to check the format of email addresses, but PHP has a validation filter that attempts to do it. 检查电子邮件地址的格式非常困难,但是PHP具有尝试执行此操作的验证过滤器 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 2.检查域名是否存在DNS记录

If a DNS (Domain Name System) record exists then at least someone has set it up. 如果存在DNS(域名系统)记录,则至少有人进行了设置。 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 3.发送验证电子邮件到该地址

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. 如果确认电子邮件未按顺序回复(例如3个小时),则地址可能存在问题。

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. 您可以使用Telnet和MX验证“已用或真实”电子邮件,并在此处记录更多信息,因为PHP存在一个很棒的库调用php-smtp-email-validation ,可以简化过程。

You create a boolean function with the file example1.php and call it when you'll validate the email text. 您使用文件example1.php创建一个布尔函数,并在验证电子邮件文本时调用它。 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 对于Gmail,Hotmail,Outlook,实时和MSM,我没有任何问题,但是对于Yahoo和Terra,该库无法正确验证电子邮件

The problem is gmail uses port 25 for their smtp outgoing mails, the other providers use different ports for their connection. 问题是gmail将25端口用于其smtp外发邮件,其他提供商则将不同的端口用于其连接。 Your response seems okay for gmail.com . 对于gmail.com您的回复似乎还可以。

When you connect to gmail smtp it gives you response 250 2.1.5 OK j5si2542844pbs.271 - gsmtp 当您连接到gmail smtp时,它会给您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. 但是,当您连接到不使用端口25的任何其他smtp时,它将给出null响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM