首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用php验证电子邮件以了解服务器上是否确实存在电子邮件

使用php验证电子邮件以了解服务器上是否确实存在电子邮件
EN

Stack Overflow用户
提问于 2018-06-06 14:55:43
回答 1查看 847关注 0票数 -1

我正在尝试做一个函数,在那里我可以检查电子邮件是否真的存在,我搜索了各种论坛,但代码只适用于gmail,而不是其他人。我正在检查mx记录,我可以做些什么来连接到那些mx记录,并询问服务器电子邮件是否真的存在。我已经添加了很多代码,这样您就可以理解我需要做什么了谢谢

代码语言:javascript
复制
private function verifyEmail($toemail, $fromemail, $getdetails = false)
    {
        $result='';
        $details=' ';
        // Get the domain of the email recipient
        $email_arr = explode('@', $toemail);
        $domain = array_slice($email_arr, -1);
        $domain = $domain[0];

        // Trim [ and ] from beginning and end of domain string, respectively
        $domain = ltrim($domain, '[');
        $domain = rtrim($domain, ']');

        if ('IPv6:' == substr($domain, 0, strlen('IPv6:'))) {
            $domain = substr($domain, strlen('IPv6') + 1);
        }

        $mxhosts = array();
        // Check if the domain has an IP address assigned to it
        if (filter_var($domain, FILTER_VALIDATE_IP)) {
            $mx_ip = $domain;
        } else {
            // If no IP assigned, get the MX records for the host name
            getmxrr($domain, $mxhosts, $mxweight);
        }

        if (!empty($mxhosts)) {
            $mx_ip = $mxhosts[array_search(min($mxweight), $mxhosts)];
        } else {
            // If MX records not found, get the A DNS records for the host
            if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
                $record_a = dns_get_record($domain, DNS_A);
                // else get the AAAA IPv6 address record
            } elseif (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
                $record_a = dns_get_record($domain, DNS_AAAA);
            }

            if (!empty($record_a)) {
                $mx_ip = $record_a[0]['ip'];
            } else {
                // Exit the program if no MX records are found for the domain host
                $result = 'Invalid Email MX ';
                $details .= 'No suitable MX records found.';

                return ((true == $getdetails) ? array($result, $details) : $result);
            }
        }

        // Open a socket connection with the hostname, smtp port 25
        $connect = @fsockopen($mx_ip, 25);

        if ($connect) {

            // Initiate the Mail Sending SMTP transaction
            if (preg_match('/^220/i', $out = fgets($connect, 1024))) {

                // Send the HELO command to the SMTP server
                fputs($connect, "HELO $mx_ip\r\n");
                $out = fgets($connect, 1024);
                $details .= $out."\n";

                // Send an SMTP Mail command from the sender's email address
                fputs($connect, "MAIL FROM: <$fromemail>\r\n");
                $from = fgets($connect, 1024);
                $details .= $from."\n";

                // Send the SCPT command with the recepient's email address
                fputs($connect, "RCPT TO: <$toemail>\r\n");
                $to = fgets($connect, 1024);
                $details .= $to."\n";

                // Close the socket connection with QUIT command to the SMTP server
                fputs($connect, 'QUIT');
                fclose($connect);

                // The expected response is 250 if the email is valid
                if (!preg_match('/^250/i', $from) || !preg_match('/^250/i', $to)) {
                    $result = 'Invalid Email Address';
                } else {
                    $result = 'Valid Email Address';
                }
            }else{echo 'layht';}
        } else {
            $result = 'Invalid Email Address';
            $details .= 'Could not connect to server';
        }
        if ($getdetails) {
            return array($result, $details);
        } else {
            return $result;
        }
    }
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50714005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档