从 PHP 中的 MX 记录确定主机名/IP 地址

发布于 2024-09-02 17:09:47 字数 757 浏览 2 评论 0原文

有一个基本的电子邮件域验证脚本,该脚本获取用户的电子邮件域,从中解析 IP 地址,然后根据各种已发布的黑名单进行检查。以下是我确定 IP 的方法:

$domain = substr(strchr($email, '@'), 1);
$ip     = gethostbyname($domain);

问题是某些电子邮件地址域,例如 [ email protected],使用 MX 记录而不是 A 记录,因此使用 gethostbyname('alumni.example.net') 将无法解析。我知道用户的电子邮件何时通过使用 PHP checkdnsrr 函数在电子邮件本身中使用 MX,但一旦到了那个阶段,我就有点不知道如何继续。

理论上,我可以解析出“根”域,即“example.net”并检查它,但是当用户可以轻松拥有 [电子邮件受保护]...

那么,关于如何最好地解决这个问题的任何建议?

have a basic email domain validation script that takes a user's email domain, resolves the IP address from that and then checks that against various published blacklists. Here is how I am determining the IP:

$domain = substr(strchr($email, '@'), 1);
$ip     = gethostbyname($domain);

The problem is that some email address domains, such as [email protected], use an MX record rather than an A record, so using gethostbyname('alumni.example.net') will fail to resolve. I know when a user's email is using an MX in the email itself by using the PHP checkdnsrr function, but once at that stage am a little stuck as to how to proceed.

In theory, I could parse out the 'root' domain, i.e. 'example.net' and check it, but I've not found reliable regex that can handle this task when the user could easily have an email the format of [email protected]...

So, any suggestions on how to best tackle this??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

对岸观火 2024-09-09 17:09:47

不要使用 gethostbyname,而是使用 dns_get_record,类似dns_get_record($domain,DNS_MX)。请参阅文档了解返回值的结构。

Instead of using gethostbyname, use dns_get_record, something like dns_get_record($domain,DNS_MX). See the docs for how the return values are structured.

陌生 2024-09-09 17:09:47
$Arr = dns_get_record('ford.com' , DNS_MX);


$count = count($Arr);
for($i=0; $i<$count; $i++) {

    echo $i.'-'.$Arr[$i]['target'].'-'.gethostbyname($Arr[$i]['target']).'-'.$Arr[$i]['ttl'].'<br/>';
}

我按照以下顺序得到了 ip 地址的结果(Pref、主机、ip、ttl)。

0-cluster4a.us.messagelabs.com-85.158.139.103-453
1-cluster4.us.messagelabs.com-216.82.242.179-453
$Arr = dns_get_record('ford.com' , DNS_MX);


$count = count($Arr);
for($i=0; $i<$count; $i++) {

    echo $i.'-'.$Arr[$i]['target'].'-'.gethostbyname($Arr[$i]['target']).'-'.$Arr[$i]['ttl'].'<br/>';
}

I got the result with ip address as following order(Pref, host, ip, ttl).

0-cluster4a.us.messagelabs.com-85.158.139.103-453
1-cluster4.us.messagelabs.com-216.82.242.179-453
深爱成瘾 2024-09-09 17:09:47

最简单的可能是

if (!getmxrr($host, $result)) {
  $result=array($host);
}

then 循环结果,调用 gethostbyname() 并检查是否没有任何结果被列入黑名单(或者您可以选择权重最低的结果,但这可以很容易地用来规避黑名单)。

我质疑将目的地列入黑名单的用处; DNS 垃圾邮件黑名单通常是针对黑名单来源制定的。

The easiest is probably

if (!getmxrr($host, $result)) {
  $result=array($host);
}

Then loop over the results, calling gethostbyname() and checking that none are blacklisted (or you could pick the result with the lowest weight, but that could be easily used to circumvent the blacklist).

I'd question the usefulness of blacklisting a destination; DNS spam blacklists are usually made for blacklisting sources.

风透绣罗衣 2024-09-09 17:09:47

您不能仅根据某人的电子邮件地址进行源验证,因为(通常)互联网上任何地方的任何一方都可以发送包含其他人的电子邮件地址的任何电子邮件。

You cannot do source validation based solely on someone's e-mail address, because (in general) any party anywhere on the internet can send any e-mail with anyone else's e-mail address in it.

秋凉 2024-09-09 17:09:47

使用此功能,您只能检查给定域的至少一个可用 mx 记录。代码未针对多个域进行测试。

function mxrecordValidate($email){
        list($user, $domain) = explode('@', $email);
        $arr= dns_get_record($domain,DNS_MX);
        if($arr[0]['host']==$domain&&!empty($arr[0]['target'])){
                return $arr[0]['target'];
        }
}
$email= '[email protected]';

if(mxrecordValidate($email)) {
        echo('This MX records exists; I will accept this email as valid.');
}
else {
        echo('No MX record exists;  Invalid email.');
}

如果发现此功能有任何改进,欢迎评论。

Using this function you can only check atleast one mx records is available for the given domain. Code is not tested with multiple domains.

function mxrecordValidate($email){
        list($user, $domain) = explode('@', $email);
        $arr= dns_get_record($domain,DNS_MX);
        if($arr[0]['host']==$domain&&!empty($arr[0]['target'])){
                return $arr[0]['target'];
        }
}
$email= '[email protected]';

if(mxrecordValidate($email)) {
        echo('This MX records exists; I will accept this email as valid.');
}
else {
        echo('No MX record exists;  Invalid email.');
}

If find any improvement in this function, comments are appreciated.

仲春光 2024-09-09 17:09:47

尝试:

$result = shell_exec ('host -t MX '.$domain);

var_dump ($result);

或者

exec ('host -t MX '.$domain, $result = array ());

var_dump ($result);

您将获得 MX 记录列表,您可以解析它并使用 gethostbyname() 检查每条记录。

编辑

Ycros提到的dns_get_record()会更好。

Try:

$result = shell_exec ('host -t MX '.$domain);

var_dump ($result);

or

exec ('host -t MX '.$domain, $result = array ());

var_dump ($result);

You will get list of MX records, you can parse it and check each record with gethostbyname().

Edit

dns_get_record() mentioned by Ycros will be better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文