龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php 使用php获得邮件服务器的IP地址

时间:2014-06-27 02:09来源:网络整理 作者:网络 点击:
分享到:
使用php获得邮件服务器的IP地址 有时候我们需要判断用户输入的邮箱地址是否是正确的,但是又不能给每个用户都发一封邮件,有一个中间办法是判断用户输入的邮箱的域名对应的

有时候我们需要判断用户输入的邮箱地址是否是正确的,但是又不能给每个用户都发一封邮件,有一个中间办法是判断用户输入的邮箱的域名对应的邮件服务器是否存在。

在php中可以通过MX lookup获得邮箱服务器的ip。如下实现的代码

// valid tests
echo 'google.com    : ' . mailIPAddress('google.com') . "\n";
echo 'microsoft.com : ' . mailIPAddress('microsoft.com') . "\n";
echo 'facebook.com  : ' . mailIPAddress('facebook.com') . "\n";
// invalid test
echo 'fyjit4w9.com  : ' . mailIPAddress('fyjit4w9.com') . "\n";

/**
 * Returns an IP address that mail can be delivered to for the passed in domain.
 *
 * First a check is done to find the MX record with the highest priority. If no
 * MX records are found, will then do an A record lookup. If no records are 
 * found, false is returned.
 *
 * Note that an IP address being returned is no guarantee that there is a mail
 * server listening at that IP address, or that it will accept mail for the 
 * domain being queried. It simply returns values based on the DNS records.
 *
 * @param $domain string The domain to query
 * @return bool|string Returns false if an MX/A record could not be found. 
 *   Returns the dotted IPv4 address if one was found.
 */
function MailIPAddress($domain) {

    $ip = false;

    // first try to get MX records

    // the lower the 'pri' value (priority) of MX hosts, the higher its 
    // precedence. if there are 3 MX records for a domain with priority 
    // 10, 20 and 30, a mail server should attempt delivery to that with
    // priority 10 first. if that fails, then 20, and so on. the numeric
    // value in the MX record is abitrary and there's no standard for what
    // it should be set to. the values could just as easily be 1, 2, 3 
    // but are typically 10, 20, 30.

    // the order of records in the array returned by dns_get_record is not
    // necessarily in order of priority, so we have to loop through the
    // array and work out which has the highest priority. this is done
    // with the $priority variable and doing a comparison on each loop
    // to see if this record has a higher priority than the previous ones

    $records = dns_get_record($domain, DNS_MX);
    $priority = null;
    foreach($records as $record) {
        if($priority == null || $record['pri'] < $priority) {
            $myip = gethostbyname($record['target']);
            // if the value returned is the same, then the lookup failed
            if($myip != $record['target']) {
                $ip = $myip;
                $priority = $record['pri'];
            }
        }
    }

    // if no MX record try A record

    // if no MX records exist for a domain, mail servers are supposed to 
    // attempt delivery instead to the A record for the domain. the final
    // check done here is to see if an A record exists, and if so, that
    // will be returned

    if(!$ip) {
        $ip = gethostbyname($domain);
        // if the value returned is the same, then the lookup failed
        if($ip == $domain) {
            $ip = false;
        }
    }

    return $ip;

}

下面我们看如何利用上面的方法做邮箱验证。

首先需要从email地址中获得域名,然后根据上面的函数判断该域名是否有对应的MX记录

$email = 'chris@outofmemory.cn';
list($emailPart, $domainPart) = explode('@', $email);
if(filter_var($email, FILTER_VALIDATE_EMAIL) && MailIPAddress($domainPart)) {
    // it's valid so do something
}
else {
    // it's not valid so do something else
}
精彩图集

赞助商链接