PHP 8.5.0 Beta 3 available for testing

getmxrr

(PHP 4, PHP 5, PHP 7, PHP 8)

getmxrrErmittelt die zu einem Internet-Hostnamen passenden MX-Records

Beschreibung

getmxrr(string $hostname, array &$hosts, array &$weights = null): bool

Sucht im DNS nach MX-Records, die zu hostname passen.

Parameter-Liste

hostname

Der Internet-Hostname.

hosts

Eine Liste der gefundenen MX-Records wird im Array hosts abgelegt.

weights

Wenn ein weights-Array übergeben wurde, werden darin die Gewichtungsinformationen gesammelt.

Rückgabewerte

Gibt true zurück, wenn mindestens ein Eintrag gefunden wurde. Wurde kein Eintrag gefunden oder trat ein Fehler auf, wird false zurückgegeben.

Anmerkungen

Hinweis:

Diese Funktion sollte nicht zur Adressverifikation verwendet werden. Nur die im DNS gefundenen Mailserver werden zurückgegeben; daher sollte entsprechend » RFC 2821 der hostname selbst mit der Priorität 0 als einziger MX verwendet werden, wenn kein MX-Record gefunden wurde.

Hinweis:

Die Funktion stand unter Windows nicht immer zur Verfügung. Verwenden Sie daher ggf. die » PEAR-Klasse » Net_DNS.

Siehe auch

  • checkdnsrr() - Prüft DNS-Einträge auf Übereinstimmung mit einem gegebenen Internet-Hostnamen oder einer IP-Adresse
  • dns_get_record() - Liefert die zu einem Hostnamen gehörenden DNS-Einträge
  • gethostbyname() - Ermittelt die zum angegebenen Internet-Hostnamen passende IPv4-Adresse
  • gethostbynamel() - Liefert eine Liste von IPv4-Adressen passend zum angegebenen Internet-Hostnamen
  • gethostbyaddr() - Liefert den zur angegebenen IP-Adresse passenden Internet-Hostnamen
  • die named(8)-Manpage

add a note

User Contributed Notes 19 notes

up
19
Robert Imhoff
14 years ago
I tried using getmxrr() to validate the domain portion of email addresses in enquiry submission forms, and there is a curious effect with some top-level domains when checking non-existant domains.

With sdlkfjsdl.com, since the domain does not exist, getmxrr() returns false, as expected, and the returned mxhosts array is empty.

But with sdlkfjsdl.gov, getmxrr() returns true, and the returned mxhosts array contains one element: NULL

With sdlkfjsdl.org, getmxrr() returns true, and the returned mxhosts array contains one element: '0.0.0.0'

With sdlkfjsdl.co.uk, getmxrr() returns true and supplies one MX record: uk-net-wildcard-null-mx.centralnic.net

So to validate the email domain, it would seem one has to check the returned mxhosts array to exclude the possibility of mxhosts being returned as NULL, 0.0.0.0 and wildcard ...
up
4
Jay
18 years ago
As stated, some of the code listed below will have trouble with multiple equal weights, such as if you query gmail.com. The following code will prevent that by switching the key/values.

<?php

// Get the records
getmxrr("gmail.com", $mx_records, $mx_weight);

// Put the records together in a array we can sort
for($i=0;$i<count($mx_records);$i++){
$mxs[$mx_records[$i]] = $mx_weight[$i];
}

// Sort them
asort ($mxs);

// Since the keys actually hold the data we want, just put those in an array, called records
$records = array_keys($mxs);

// Simply echoes all the stuff in the records array
for($i = 0; $i < count($records); $i++){
echo
$records[$i];
echo
'<br>';
}

?>

If you wanted to get the weight, you would use "array_values($mxs);" instead of "array_keys($mxs);".

Hope this helps some people.
up
11
rune dot heggtveit at devzone dot progative dot com
20 years ago
An other way to do mx-lookup on a windows platform.
Rewrote this from an other class i wrote for DNS lookup - so it might be a bit messy - but hope you get the idea.

Big thanks to the rfc community.

<?php

class mxlookup
{
var
$dns_socket = NULL;
var
$QNAME = "";
var
$dns_packet= NULL;
var
$ANCOUNT = 0;
var
$cIx = 0;
var
$dns_repl_domain;
var
$arrMX = array();

function
mxlookup($domain, $dns="192.168.2.1")
{
$this->QNAME($domain);
$this->pack_dns_packet();
$dns_socket = fsockopen("udp://$dns", 53);

fwrite($dns_socket,$this->dns_packet,strlen($this->dns_packet));
$this->dns_reply = fread($dns_socket,1);
$bytes = stream_get_meta_data($dns_socket);
$this->dns_reply .= fread($dns_socket,$bytes['unread_bytes']);
fclose($dns_socket);
$this->cIx=6;
$this->ANCOUNT = $this->gord(2);
$this->cIx+=4;
$this->parse_data($this->dns_repl_domain);
$this->cIx+=7;

for(
$ic=1;$ic<=$this->ANCOUNT;$ic++)
{
$QTYPE = ord($this->gdi($this->cIx));
if(
$QTYPE!==15){print("[MX Record not returned]"); die();}
$this->cIx+=9;
$mxPref = ord($this->gdi($this->cIx));
$this->parse_data($curmx);
$this->arrMX[] = array("MX_Pref" => $mxPref, "MX" => $curmx);
$this->cIx+=3;
}
}

function
parse_data(&$retval)
{
$arName = array();
$byte = ord($this->gdi($this->cIx));
while(
$byte!==0)
{
if(
$byte==192) //compressed
{
$tmpIx = $this->cIx;
$this->cIx = ord($this->gdi($cIx));
$tmpName = $retval;
$this->parse_data($tmpName);
$retval=$retval.".".$tmpName;
$this->cIx = $tmpIx+1;
return;
}
$retval="";
$bCount = $byte;
for(
$b=0;$b<$bCount;$b++)
{
$retval .= $this->gdi($this->cIx);
}
$arName[]=$retval;
$byte = ord($this->gdi($this->cIx));
}
$retval=join(".",$arName);
}

function
gdi(&$cIx,$bytes=1)
{
$this->cIx++;
return(
substr($this->dns_reply, $this->cIx-1, $bytes));
}

function
QNAME($domain)
{
$dot_pos = 0; $temp = "";
while(
$dot_pos=strpos($domain,"."))
{
$temp = substr($domain,0,$dot_pos);
$domain = substr($domain,$dot_pos+1);
$this->QNAME .= chr(strlen($temp)).$temp;
}
$this->QNAME .= chr(strlen($domain)).$domain.chr(0);
}

function
gord($ln=1)
{
$reply="";
for(
$i=0;$i<$ln;$i++){
$reply.=ord(substr($this->dns_reply,$this->cIx,1));
$this->cIx++;
}

return
$reply;
}

function
pack_dns_packet()
{
$this->dns_packet = chr(0).chr(1).
chr(1).chr(0).
chr(0).chr(1).
chr(0).chr(0).
chr(0).chr(0).
chr(0).chr(0).
$this->QNAME.
chr(0).chr(15).
chr(0).chr(1);
}

}

?>

<?php

/* Exampe of use: */
$mx = new mxlookup("php.net");

print
$mx->ANCOUNT." MX Records\n";
print
"Records returned for ".$mx->dns_repl_domain.":\n<pre>";
print_r($mx->arrMX);

?>

Return:

02 MX Records Records returned for php.net:

Array
(
[0] => Array
(
[MX_Pref] => 15
[MX] => smtp.osuosl.org
)

[1] => Array
(
[MX_Pref] => 5
[MX] => osu1.php.net
)

)
up
3
zorlac_man at hotmail dot com
21 years ago
For some reason this and the other DNS lookup functions seem to be really slow on my Linux box. I've checked several things and have no explanation.

As a work-around, I gave in and just used a system call to dig:

<?php
CheckMX
("fakedomain.org");
CheckMX("hotmail.com");

function
CheckMX($domain) {
exec("dig +short MX " . escapeshellarg($domain),$ips);
if(
$ips[0] == "") {
print
"MX record found for $domain not found!\n";
return
FALSE;
}
print
"MX Record for $domain found\n";
return
TRUE;
}
?>

Output:

MX record found for fakedomain.org not found!
MX Record for hotmail.com found

As someone else pointed out, it is prudent to check to see if the TLD has an IP address if the MX record is not found.
up
1
php [spat] hm2k.org
16 years ago
I decided to have a bash at this after doing a bit of research...

<?php

// getmxrr() support for Windows by HM2K <php [spat] hm2k.org>
function win_getmxrr($hostname, &$mxhosts, &$mxweight=false) {
if (
strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') return;
if (!
is_array ($mxhosts) ) $mxhosts = array();
if (empty(
$hostname)) return;
$exec='nslookup -type=MX '.escapeshellarg($hostname);
@
exec($exec, $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)) {
$mxweight[$i] = trim($parts[1]);
$mxhosts[$i] = trim($parts[2]);
}
if (
preg_match('/responsible mail addr = (.+)$/i', $line, $parts)) {
$mxweight[$i] = $i;
$mxhosts[$i] = trim($parts[1]);
}
}
return (
$i!=-1);
}