如何获得mx记录的域名

问题描述:

我想获取mx记录的详细信息...但我不知道如何获取我的代码中的mx记录目标... 我得到mx记录的详细信息。如何在我的代码中显示mx目标... 使用PHP,ping一个SMTP服务器并检查MX记录?我愿意写一个脚本这样的:如何获得mx记录的域名

我的代码是:

// Queries the DNS server for MX entries of a certain domain. 
protected function mx_query($domain) { 
    $hosts = array(); 
    $weight = array(); 
    if (function_exists('getmxrr')) { 
     getmxrr($domain, $hosts, $weight); 
    } else { 
     $this->getmxrr($domain, $hosts, $weight); 
    } 
    return array($hosts, $weight); 
} 

// Provides a windows replacement for the getmxrr function. 
protected function getmxrr($hostname, &$mxhosts, &$mxweights) { 
    if (!is_array($mxhosts)) { 
     $mxhosts = array(); 
    } 
    if (!is_array($mxweights)) { 
     $mxweights = array(); 
    } 
    if (empty($hostname)) { 
     return; 
    } 
    $cmd = 'nslookup -type=MX ' . escapeshellarg($hostname); 
    if (!empty($this->mx_query_ns)) { 
     $cmd .= ' ' . escapeshellarg($this->mx_query_ns); 
    } 
    exec($cmd, $output); 
    if (empty($output)) { 
     return; 
    } 
    $i = -1; 
    foreach ($output as $line) { 
     $i++; 
     if (preg_match("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.+)$/i", $line, $parts)) { 
      $mxweights[$i] = trim($parts[1]); 
      $mxhosts[$i] = trim($parts[2]); 
     } 
     if (preg_match('/responsible mail addr = (.+)$/i', $line, $parts)) { 
      $mxweights[$i] = $i; 
      $mxhosts[$i] = trim($parts[1]); 
     } 
    } 
    return ($i != -1); 
} 

     if($this->check_mx){ 
     $check_mx = $this->check_mx($domain); 
     if(!$check_mx){ 
      $output['result']['errors']['check_mx'] = 'Domain failed MX check.'; 
      $output['result']['success'] = 0; 
     }else{ 
      $output['result']['report']['check_mx'] = $output['result']['report']['target']; 


     } 
    } 

在PHP dns_get_record(和dns_get_mx)方法(和在documentation pages给出的例子)看看。