How to check programmatically if an email is existing or not

前端 未结 3 1148
攒了一身酷
攒了一身酷 2020-12-01 17:32

How to write a code to check an email is existing or not? For example donkey_sdh123@gmail.com, donkey_sdh123@yahoo.com, or donkey_sdh123@lycos.com all these emails do not ex

相关标签:
3条回答
  • 2020-12-01 17:58

    It is possible to connect to a remote smtp server via telnet:

    http://www.yuki-onna.co.uk/email/smtp.html

    The only thing is that many mail servers will not accept smtp (e.g. yahoo), and since information about whether an address is valuable to spammers, making it easy to discover which email addresses exist is not in the interests of any email host.

    You can do a basic check on whether the domain exists using ping or other network scans, and you could use the Google API to search for an email address in case it is listed on the internet in the clear. But it's you and all the scammers out there doing the same thing, so it's probably not worth trying.

    Another thing to note is that many email providers permit users to provide derivative, temporary or alias addresses, which will work for some limited time or use, but which are not mailboxes themselves. In this circumstance even a checker that worked would think the address did not exist even though the user would receive an email sent to it.

    Since expired addresses probably aren't released (to prevent people getting previous users' mail), there will then be sources of false positives (check says yes but is wrong), as well as the false negatives (check says no but is wrong), along with all the friction of being treated like a spammer.

    0 讨论(0)
  • 2020-12-01 18:17

    See this code i got it from web.

     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;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 18:19

    In short: it is not possible. At most you can try to check if the domain in question have MX record and try to connect to its mail server. Even that won't guarantee it is in working condition though.

    You absolutely can't check if a particular e-mail exists on it in some standartized way as there are many approaches to masquerade and aliasing that many server employ. Servers can and will report in SMTP exchange non-existent addresses as valid for many reasons in both VRFY and MAIL/RCPT. The only definite answer you can get is that e-mail is invalid if it is rejected by MAIL/RCPT, but being accepted is not definite proof of it being valid, as it can be rejected down the line of e-mail processing. Abusing MAIL/RCPT without actually sending anything can also lead to you being blocked.

    If you want to verify user-supplied e-mail, your best bet is to send confirmation letter there.

    You also should review if you really need confirmed working e-mail at all.

    0 讨论(0)
提交回复
热议问题