如何以编程方式检查电子邮件是否存在 [英] How to check programmatically a email is existing or not

查看:198
本文介绍了如何以编程方式检查电子邮件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写代码以检查电子邮件是否存在? ,例如donkey_sdh123@gmail.com或donkey_sdh123@yahoo.com,或者donkey_sdh123@lycos.com,所有这些电子邮件都不存在于Gmail,yahoo和lycos数据库中。

How to write a code to check a email is existing or not ? , For example donkey_sdh123@gmail.com, or donkey_sdh123@yahoo.com, or donkey_sdh123@lycos.com all this email is not exist in gmail, yahoo and lycos database.


看到屏幕截图,donkey_sdh123@gmail.com不好意味着它不存在。如何在我的项目中实现相同的事情?

See the screen shot, donkey_sdh123@gmail.com is bad means its not existing. how can i implement same thing in my project.?

javascript,jquery shell脚本,c或c ++是欢迎的。使用.net。

javascript,jquery shell script, c or c++ are welcome. With out .net.

提前感谢

推荐答案

 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;
    }
}

这篇关于如何以编程方式检查电子邮件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆