int getmxrr ( string $hostname , array $mxhosts [, array $ weight ] )

Searches DNS for MX records corresponding to hostname 에 대응이 되는 MX 레코드를 찾기 위해 DNS를 찾습니다. 레코드 값이 있으면 true를 없으면 false를 반환합니다.

윈도우에서 nslookup.exe에 mx값을 요청 했을때 출력입니다. 이를 기초로 함수를 작성할 수 있습니다.


<?php
 // 윈도우환경 일때
 if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    function getmxrr($host, &$hosts, &$weight=false) {
       $hosts = array();
       $weight = array();
       $preference = array();
       $records = array();
       exec('%SystemRoot%\System32\nslookup.exe -q=mx '.escapeshellarg($host), $arr);
       foreach($arr as $line) {
          if (preg_match("/.*mail exchanger = (.*)/",
                          $line, $records)) {
     $hosts[] = trim($records[1]);
    }
    if (preg_match("/.*MX preference = ([0-9]+)/",
                          $line, $preference)) {
     $weight[] = trim($preference[1]);
    }
       }
       return( count($hosts) > 0 );
   }
 }
 else {
   getmxrr($host, $mxhosts, $weight);
 }

 /*
    $mxhosts = mail exchanger 값
    $weight = MX preference 값
 */
 print_r(array($mxhosts,$weight);

 /* 결과
 Array
 (
    [0] => Array
        (
            [0] => m.mx.mail.yahoo.com
            [1] => a.mx.mail.yahoo.com
            [2] => b.mx.mail.yahoo.com
            [3] => c.mx.mail.yahoo.com
            [4] => d.mx.mail.yahoo.com
            [5] => e.mx.mail.yahoo.com
            [6] => f.mx.mail.yahoo.com
            [7] => g.mx.mail.yahoo.com
            [8] => h.mx.mail.yahoo.com
            [9] => i.mx.mail.yahoo.com
            [10] => j.mx.mail.yahoo.com
            [11] => k.mx.mail.yahoo.com
            [12] => l.mx.mail.yahoo.com
        )
    [1] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
            [4] => 1
            [5] => 1
            [6] => 1
            [7] => 1
            [8] => 1
            [9] => 1
            [10] => 1
            [11] => 1
            [12] => 1
        )
 )
 */
 ?>

0 댓글