PHP dns_get_mx() Function: Everything You Need to Know
As a PHP developer, you may need to obtain the mail exchange (MX) records for a domain name. In such scenarios, the PHP dns_get_mx() function comes in handy. It
As a PHP developer, you may need to obtain the mail exchange (MX) records for a domain name. The dns_get_mx() function was historically used for this purpose, but it was removed in PHP 8.2. The modern approach uses dns_get_record() with the DNS_MX flag. In this article, we will cover the legacy function, its deprecation, and how to use the recommended alternative.
What is the dns_get_mx() Function?
The dns_get_mx() function was a built-in PHP function that retrieved the mail exchange (MX) records for a given domain name. MX records tell other mail servers which hosts are responsible for accepting email for a domain, ranked by priority. dns_get_mx() was an alias of getmxrr() and was removed in PHP 8.2. The recommended replacement is dns_get_record($hostname, DNS_MX).
The Legacy dns_get_mx() Syntax
The original (now removed) function was an alias of getmxrr() and used reference parameters to return its results:
dns_get_mx($hostname, &$mxhosts, &$weight);The function took three parameters:
$hostname: The domain name for which you want to retrieve the MX records.$mxhosts: A reference variable filled with the list of MX host names.$weight: An optional reference variable filled with the priority (preference) of each host.
Because the results came back through references rather than a return value, the function only returned true on success or false on failure. This older pattern is exactly why the function was retired in favor of the array-returning dns_get_record().
How to Retrieve MX Records Today
The modern approach is dns_get_record($hostname, DNS_MX), which returns an array of associative arrays — one per record — and works on every supported PHP version:
Here we retrieve the MX records for gmail.com. dns_get_record() returns an array of records on success, or false on failure. We loop through them with foreach and print the mail server host and its priority. Note that DNS queries can hang or fail on invalid domains, so always check the return value and consider timeout handling in production.
Understanding the returned array
Each MX record is an associative array. The keys you will use most often are:
target: the host name of the mail server that should receive mail for the domain.pri: the priority (preference) — lower numbers are tried first. This is the key that replaces the old$weightreference parameter; note it ispri, notpriority.host: the domain the record belongs to.type: the record type, here alwaysMX.ttl: how long (in seconds) the record may be cached.
Sorting by priority
DNS does not guarantee the records come back in priority order, so sort them yourself before choosing a mail server:
<?php
$records = dns_get_record("gmail.com", DNS_MX);
usort($records, fn($a, $b) => $a['pri'] <=> $b['pri']);
foreach ($records as $record) {
echo $record['pri'] . " => " . $record['target'] . "\n";
}A mail client tries the lowest-priority host first and falls back to higher numbers only if it is unreachable.
Related Functions
dns_get_record()— the general-purpose replacement; passDNS_MX,DNS_A,DNS_TXT, etc.getmxrr()— the reference-parameter equivalent thatdns_get_mx()aliased.checkdnsrr()— checks whether a record of a given type exists for a host.
Conclusion
While dns_get_mx() was once the standard for retrieving MX records, it has been removed in PHP 8.2. Use dns_get_record($hostname, DNS_MX) instead for modern, reliable DNS queries. We hope this guide helps you implement MX record lookups effectively in your PHP applications.