Checking for an MX record with getmxrr() or dns_get_record() returns incorrect information

946 Views Asked by At

I am currently attempting to check the validity of an email address by first checking if an MX record exists, using getmxrr() or dns_get_record() with PHP 7 on a DigitalOcean droplet.

In my example (which returns the incorrect response), I am attempting to check an email address on the domain “nuwatches.com”. Using dns_get_record(”nuwatches.com”, DNS_ALL), I’m returned an array that DOES include an MX record, even though I know this does not exist in reality:

[5] => Array (
    [host] => nuwatches.com.com
    [class] => IN
    [ttl] => 27
    [type] => MX
    [pri] => 1
    [target] => mail.user-mail.net
)

However, if I use nslookup directly from the console on the DigitalOcean droplet, I’m correctly told that no MX record exists.

In addition, if I run the dns_get_record() function on a different droplet, or using an online PHP code runner, I’m also given the correct answer (which is that no MX record exists).

I’ve spoken to DigitalOcean support and they suggest there’s something strange happening with PHP on that particular droplet, perhaps with caching issues, but I can’t at all figure out what might be causing the discrepancy, especially as I'm not caching anything myself.

As it stands, my only option to get the correct response on this droplet is to run nslookup and then parse the result, but I would like to use dns_get_record() if possible.

I’d appreciate any suggestions. Thanks!

2

There are 2 best solutions below

0
On

You can try to use a 3rd party PHP library for that. I've just tested it and it worked for me.

bluelibraries/dns

sample

use BlueLibraries\Dns\Facade\DNS;
use BlueLibraries\Dns\Records\RecordTypes;

$records = DNS::getRecords('nuwatches.com', RecordTypes::MX);

print_r($records);

result

Array
(
    [0] => BlueLibraries\Dns\Records\Types\MX Object
        (
            [data:protected] => Array
                (
                    [host] => nuwatches.com
                    [ttl] => 3600
                    [class] => IN
                    [type] => MX
                    [pri] => 1
                    [target] => localhost
                )

        )

)
0
On

I am having similar problems with dns_get_record() when calling it with the default value for record type DNS_ANY or when calling it with DNS_ALL.

php dns_get_records( host, type ) is returning false with type=DNS_ALL but returns DNS records with DNS_TXT or DNS_A

A solution that worked for me was to use:

dns_get_record( ”nuwatches.com”, DNS_MX )

I am still investigating the reason behind this behaviour.