checkdnsrr() can test whether a DNS record exists, while getmxrr() retrieves mail-exchanger hosts. DNS results are useful validation signals but do not prove that an email address or remote service is usable. If the domain comes from a request, validate and normalize it before making the DNS lookup.
<?php
$domain = 'example.com';
if (checkdnsrr($domain, 'MX')) {
$hosts = [];
$weights = [];
getmxrr($domain, $hosts, $weights);
print_r($hosts);
} else {
echo 'No MX record found.';
}
?>$domain="plus2net.com";
if(!checkdnsrr($domain,'MX')) {
echo "Not there ";
}else {
echo " It is there ";
}
getmxrr(hostname, array $result, array $weight)
If returns True of False based on the information found. Output is stored in $result as array.
$domain='example.com';
if(getmxrr($domain,$mx_details)){
foreach($mx_details as $key=>$value){
echo "$key => $value <br>";
}
}
<?php
$domain = "plus2net.com";
if (!checkdnsrr($domain, 'MX')) {
echo "Domain does not exist.";
} else {
echo "Domain exists.";
}
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.