如何以编程方式检查电子邮件是否存在

SR *_*ery 3 email

如何编写代码来检查电子邮件是否存在?例如,donkey_sdh123 @ gmail.com,donkey_sdh123 @ yahoo.com或donkey_sdh123@lycos.com所有这些电子邮件都不存在于gmail,yahoo和lycos数据库中.
在此输入图像描述


查看截图.donkey_sdh123@gmail.com很糟糕.这意味着它不存在.我怎样才能在我的项目中实现相同的功能?

欢迎使用javascript,jquery shell脚本,c或c ++.没有.net.

Ole*_*kov 6

简而言之:这是不可能的.最多可以尝试检查相关域是否有MX记录并尝试连接到其邮件服务器.即使这样也不能保证它处于工作状态.

你绝对无法以某种标准化的方式检查是否存在特定的电子邮件,因为许多服务器采用了许多伪装和别名的方法.服务器可以并且将在SMTP中交换不存在的地址,因为有很多原因VRFY并且MAIL/ 和/ RCPT.你可以得到的唯一明确的答案是,如果电子邮件被MAIL/ 拒绝,则电子邮件无效RCPT,但被接受并不能证明它是有效的,因为它可能会被拒绝接收电子邮件.滥用MAIL/ RCPT不实际发送任何内容也可能导致您被阻止.

如果您想验证用户提供的电子邮件,最好的办法是在那里发送确认信.

您还应该检查是否确实需要确认的工作电子邮件.

  • 关键词:一些标准化的方式.例如,GMail将用户名+adbitrary_tag@gmail.com的邮件发送到username@gmail.com,其他一些服务也可以这样做,但并非所有服务都会响应"VRFY"告诉该地址有效.另一种方式 - 垃圾邮件`MAIL`和`RCPT`可能会让你被阻止.因此,确认信是检查该地址是否存在的唯一方法,它是**真正的预期用户地址**. (3认同)
  • @Sreenivas:服务*声称*它能够做某事的事实并不意味着它是可能的。有卖蛇油的。尽管这取决于您所说的“电子邮件存在”是什么意思。 (2认同)

odi*_*apc 5

当我需要测试电子邮件是否存在时,我总是回到下面的文章:http://www.webdigi.co.uk/blog/2009/how-to-check-if-an-email-address-exists-不,送的电子邮件/

  • 你应该使用SMTP.您可以使用任何编程的telnet客户端.您甚至可以使用bash脚本执行此操作. (2认同)

SR *_*ery 1

请参阅我从网络上获得的这段代码。

 class SmtpValidator {

    private $options = array(
            "port" => 25,
            "timeout" => 1,  // Connection timeout to remote mail server.
            "sender" => "info@webtrafficexchange.com",
            "short_response" => false,
    );

    /**
     *  Override the options for those specified.
     */
    function __construct($options = null) {
        if (!empty($options)) {
            if (is_array($options)) {
                foreach ($options as $key => $value) {
                    $this->options[$key] = $value;
                }
            }
        }
    }

    /**
     *  Validate the email address via SMTP.
     *  If 'shore_response' is true, the method will return true or false;
     *  Otherwise, the entire array of useful information will be provided.
     */
    public function validate($email, $options = null) {

        $result = array("valid" => false);
        $errors = array();

        // Email address (format) validation
        if (empty($email)) {
            $errors = array("Email address is required.\n");
        } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errors = array("Invalid email address.\n");
        } else {
            list($username, $hostname) = split('@', $email);
            if (function_exists('getmxrr')) {
                if (getmxrr($hostname, $mxhosts, $mxweights)) {
                    $result['mx_records'] = array_combine($mxhosts, $mxweights);
                    asort($result['mx_records']);
                } else {
                    $errors = "No MX record found.";
                }
            }

            foreach ($mxhosts as $host) {
                $fp = @fsockopen($host, $this->options['port'], $errno, $errstr, 
                                       $this->options['timeout']);
                if ($fp) {
                    $data = fgets($fp);
                    $code = substr($data, 0, 3);
                    if($code == '220') {
                        $sender_domain = split('@', $this->options['sender']);
                        fwrite($fp, "HELO {$sender_domain}\r\n");
                        fread($fp, 4096);
                        fwrite($fp, "MAIL FROM: <{$this->options['sender']}>\r\n");
                        fgets($fp);
                        fwrite($fp, "RCPT TO:<{$email}>\r\n");
                        $data = fgets($fp);
                        $code = substr($data, 0, 3);
                        $result['response'] = array("code" => $code, "data" => $data);
                        fwrite($fp, "quit\r\n");
                        fclose($fp);
                        switch ($code) {
                            case "250":  // We're good, so exit out of foreach loop
                            case "421":  // Too many SMTP connections
                            case "450":
                            case "451":  // Graylisted
                            case "452":
                                $result['valid'] = true;
                                break 2;  // Assume 4xx return code is valid.
                            default:
                                $errors[] = "({$host}) RCPT TO: {$code}: {$data}\n";
                        }
                    } else {
                        $errors[] = "MTA Error: (Stream: {$data})\n";
                    }
                } else {
                    $errors[] = "{$errno}: $errstr\n";
                }
            }
        }
        if (!empty($errors)) {
            $result['errors'] = $errors;
        }
        return ($this->options['short_response']) ? $result['valid'] : $result;
    }
}
Run Code Online (Sandbox Code Playgroud)