使用DNS检查php的电子邮件domail验证 [英] Email domail validation using DNS check in php

查看:219
本文介绍了使用DNS检查php的电子邮件domail验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在更多的是使用SMTP发送大量电子邮件。在发送邮件之前,我需要检查这些邮件ID是否有效。我有一个代码来检查它我在下面加了一个它从Github。我只需要确认,所有这些通过使用这个DNS端口检查,它是否使我们的服务器像黑名单等麻烦,当检查大量无效的邮件?


$ b $ ($ toemail,$ fromemail,$ getdetails = false){
$ email_arr = explode(@,$)$ {pre> 发邮件);
$ domain = array_slice($ email_arr,-1);
$ domain = $ domain [0];
//从域名字符串的开始和结尾分别修剪[和]
$ domain = ltrim($ domain,[);
$ domain = rtrim($ domain,]);
if(IPv6:== substr($ domain,0,strlen(IPv6:))){
$ domain = substr($ domain,strlen(IPv6)+ 1) ;
}
$ mxhosts = array();
if(filter_var($ domain,FILTER_VALIDATE_IP))
$ mx_ip = $ domain;
else
getmxrr($ domain,$ mxhosts,$ mxweight);
if(!empty($ mxhosts))
$ mx_ip = $ mxhosts [array_search(min($ mxweight),$ mxhosts)];
else {
if(filter_var($ domain,FILTER_VALIDATE_IP,FILTER_FLAG_IPV4)){
$ record_a = dns_get_record($ domain,DNS_A);
}
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 {
$ result =invalid;
$ details。=找不到合适的MX记录。
return((true == $ getdetails)?array($ result,$ details):$ result);
}
}

$ connect = @fsockopen($ mx_ip,25);
if($ connect){
if(preg_match(/ ^ 220 / i,$ out = fgets($ connect,1024))){
fputs($ connect,HELO $ mx_ip\r\\\
);
$ out = fgets($ connect,1024);
$ details。= $ out。\\\
;

fputs($ connect,MAIL FROM:< $ fromemail> \r\\\
);
$ from = fgets($ connect,1024);
$ details。= $ from。\\\
;
fputs($ connect,RCPT TO:< $ toemail> \r\\\
);
$ to = fgets($ connect,1024);
$ details。= $ to。\\\
;
fputs($ connect,QUIT);
fclose($ connect);
if(!preg_match(/ ^ 250 / i,$ from)||!preg_match(/ ^ 250 / i,$ to)){
$ result =invalid
}
else {
$ result =valid;
}
}
}
else {
$ result =invalid;
$ details。=无法连接到服务器;
}
if($ getdetails){
return array($ result,$ details);
}
else {
return $ result;
}
}
?>

如果您有任何更好的想法来帮助我,请发布&帮帮我。
提前感谢您。

解决方案

DNS查找不会导致黑名单。然而,它对您的DNS服务的质量非常敏感 - 我最近开始使用 net_dns2 而不是getmxrr(),因为后者不区分超时,NXDOMAIN和其他错误(不幸的是,这里的DNS服务器由我们的专家配置)。



也有助于知道我使用curl_multi _ *()函数来运行并发查找 - 处理大型列表可能需要一个 long 时间描述 here )。



探索MX可能会导致您被列入黑名单 - 它主要是浪费时间作为可交付性的度量(并且您正在使用的脚本明确地解析每个MX的IP地址,在性能方面有些昂贵)。此外,如果主MX不可用(SMTP被设计为异步),则会导致假阴性。



另一种解决方案是减少假阴性(和假阳性比较使用您所描述的方法)使用反弹处理程序,虽然这是费用的确定一些延迟。



我还建议(取决于处理列表所需的时间),您预先验证电子邮件地址使用正则表达式,但小心有一个很多坏例子如何在互联网上执行此操作。


I'm now more with sending bulk emails using SMTP. Before sending mails I need to check those mail id's are valid. I have got a code to check it & I have added it below. Its from the Github. I just need confirm with you all that by using this DNS port checks, does it makes our server in a trouble like Blacklisting etc.., when checking lots of mails which are not valid?

<?php
function verifyEmail($toemail, $fromemail, $getdetails = false){
    $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();
    if( filter_var($domain, FILTER_VALIDATE_IP) )
        $mx_ip = $domain;
    else
        getmxrr($domain, $mxhosts, $mxweight);
    if(!empty($mxhosts) )
        $mx_ip = $mxhosts[array_search(min($mxweight), $mxhosts)];
    else {
        if( filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ) {
            $record_a = dns_get_record($domain, DNS_A);
        }
        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 {
            $result   = "invalid";
            $details .= "No suitable MX records found.";
            return ( (true == $getdetails) ? array($result, $details) : $result );
        }
    }

    $connect = @fsockopen($mx_ip, 25); 
    if($connect){ 
        if(preg_match("/^220/i", $out = fgets($connect, 1024))){
            fputs ($connect , "HELO $mx_ip\r\n"); 
            $out = fgets ($connect, 1024);
            $details .= $out."\n";

            fputs ($connect , "MAIL FROM: <$fromemail>\r\n"); 
            $from = fgets ($connect, 1024); 
            $details .= $from."\n";
            fputs ($connect , "RCPT TO: <$toemail>\r\n"); 
            $to = fgets ($connect, 1024);
            $details .= $to."\n";
            fputs ($connect , "QUIT"); 
            fclose($connect);
            if(!preg_match("/^250/i", $from) || !preg_match("/^250/i", $to)){
                $result = "invalid"; 
            }
            else{
                $result = "valid";
            }
        } 
    }
    else{
        $result = "invalid";
        $details .= "Could not connect to server";
    }
    if($getdetails){
        return array($result, $details);
    }
    else{
        return $result;
    }
}
?>

If you have any better idea's to help me, please post & help. Thanking you in advance.

解决方案

A DNS lookup won't result in blacklisting. However it will be very sensitive to the quality of your DNS service - I've recently started using net_dns2 rather than getmxrr() as the latter does not differentiate between timeouts, NXDOMAIN and other errors (unfortunately the DNS servers here are configured by our "experts").

(it might also be helpful to know that I use the curl_multi_*() functions to run concurrent lookups - processing a large list can take a long time - described here).

Probing the MX may well result in you being blacklisted - and its mostly a waste of time as measure of deliverability (and as the script you're using explicitly resolves an IP address for each MX, somewhat expensive in terms of performance). Further it will lead to false negatives if the primary MX is unavailable (SMTP is designed to be asynchronous).

An alternative solution, reducing the false negatives (and false positives compared with the method you describe) is to use a bounce handler, although this comes at the cost of some latency in making a determination.

I'd also suggest (depending on the legth of time it takes to process the list) that you pre-validate the email address with a regex - but beware there are a lot of bad examples of how to do this on the internet.

这篇关于使用DNS检查php的电子邮件domail验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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