Home » Php » php – dns_get_mx, dns_get_record and checkdnsrr don't work with MX (work with A though)

php – dns_get_mx, dns_get_record and checkdnsrr don't work with MX (work with A though)

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to validate whether an MX record exists for a given domain, and nothing works.

// prepare the hostname
$hostname = 'gmail.com';
$hostname = idn_to_ascii($hostname);//php.net/manual/function.checkdnsrr.php#113537
$hostname = sprintf('%s.', $hostname);//php.net/manual/function.checkdnsrr.php#119969
// perform the checks
dns_get_mx($hostname, $dns_get_mx); // a.k.a. getmxrr()
$dns_get_record = dns_get_record($hostname, DNS_MX);
$checkdnsrr = checkdnsrr($hostname, 'MX');
// output the result
var_dump(array(
    'hostname' => $hostname,
    'dns_get_mx' => $dns_get_mx,
    'dns_get_record' => $dns_get_record,
    'checkdnsrr' => $checkdnsrr,
));

Here’s what I’m getting (for both gmail.com. and gmail.com, tried separately):

Warning: dns_get_record(): A temporary server error occurred. in /var/www/html/example.php
array (size=4)
  'hostname' => string 'gmail.com.' (length=10)
  'dns_get_mx' => 
    array (size=0)
      empty
  'dns_get_record' => boolean false
  'checkdnsrr' => boolean false

If I replace MX with A, dns_get_record() returns a correct IP address, and checkdnsrr() returns true.

There’s nothing in the logs (except for the above warning), and googling didn’t help. How do I debug this?

UPD The issue appears in my local VirtualBox environment (PHP 5.6.39); everything actually works on the production server (PHP 7.3.11)

How to&Answers:

VirtualBox seems to be overwriting /etc/resolv.conf, and that messes up the DNS on the guest machine.

My fix for now is:

echo "nameserver 1.1.1.1" > /etc/resolv.conf

More info