W3docs

PHP getmxrr() Function: Everything You Need to Know

As a PHP developer, you may need to obtain the mail exchange (MX) records for a given domain name. In such scenarios, the PHP getmxrr() function comes in handy.

As a PHP developer, you may need to obtain the mail exchange (MX) records for a given domain name. In such scenarios, the getmxrr() function was historically used. Important: getmxrr() was deprecated in PHP 8.2 and removed in PHP 8.4. For modern PHP applications, use dns_get_record() instead. This article explains the legacy function for maintaining older codebases.

What is the getmxrr() Function?

The getmxrr() function performs a DNS lookup on a specified domain name and returns an array of all associated MX records. It requires the dns extension to be enabled in your PHP environment. The function returns true on success and false on failure.

How to Use the getmxrr() Function

Using the getmxrr() function is straightforward. Here is the syntax of the function:

The PHP syntax of getmxrr() Function

getmxrr($hostname, &$mxhosts, &$weight);

The function takes three parameters:

  • $hostname: The domain name for which you want to retrieve the MX records.
  • $mxhosts: A reference to an array that will store the MX hosts for the domain name. (Note: In PHP 5.3+, the & symbol is only required in the function signature, not when calling the function.)
  • $weight: A reference to an array that will store the MX hosts' priority weights.

Here is an example of how to use the getmxrr() function to retrieve the MX records for a domain name:

How to Use the getmxrr() Function?

php— editable, runs on the server

In this example, we retrieve the MX records for the domain name "example.com". The function performs a DNS lookup and populates the $mxhosts and $weight arrays. We then loop through the arrays and display the MX hosts and their weights. If the lookup fails, the else block handles the error gracefully.

Understanding MX Weights (Priority)

Each MX record has a weight (also called priority or preference). When a mail server delivers email, it tries the host with the lowest weight first; higher weights act as fallbacks. So a record with weight 10 is preferred over one with weight 20.

getmxrr() does not sort the results for you — the records come back in whatever order the DNS resolver returns them. If you need them in delivery order, sort by weight yourself. Because $mxhosts and $weight are parallel arrays (index 0 of one matches index 0 of the other), the clean way to keep them aligned while sorting is to combine them first:

<?php

$hostname = "example.com";
$mxhosts = [];
$weight = [];

if (getmxrr($hostname, $mxhosts, $weight)) {
  // Pair each host with its weight, then sort by weight ascending.
  $records = array_map(fn($host, $w) => ['host' => $host, 'weight' => $w], $mxhosts, $weight);
  usort($records, fn($a, $b) => $a['weight'] <=> $b['weight']);

  foreach ($records as $record) {
    echo "Priority {$record['weight']}: {$record['host']}\n";
  }
} else {
  echo "No MX record found for $hostname";
}

Now the loop prints the most-preferred mail server first.

Return Value and Common Gotchas

  • Return type is boolean. getmxrr() returns true if at least one MX record was found and false otherwise. The actual data is written to the by-reference $mxhosts and $weight arrays — not returned.
  • No MX record is not the same as no domain. A domain can resolve (have an A record) yet have no MX records, in which case getmxrr() returns false. Per the SMTP standard, mail can still be delivered to the host's A record as a fallback, but getmxrr() won't tell you that.
  • Trailing dots. Returned host names may include a trailing dot (e.g. mail.example.com.). Trim it if you compare strings: rtrim($host, '.').
  • Requires the dns extension. On most builds this is compiled in by default, but minimal or hardened environments may disable it.

The Modern Replacement: dns_get_record()

Because getmxrr() is removed in PHP 8.4, new code should use dns_get_record() with the DNS_MX flag. It returns a single, richer array instead of two parallel ones:

<?php

$hostname = "example.com";
$records = dns_get_record($hostname, DNS_MX);

if ($records) {
  // Sort by the 'pri' (priority/weight) field, lowest first.
  usort($records, fn($a, $b) => $a['pri'] <=> $b['pri']);

  foreach ($records as $record) {
    echo "Priority {$record['pri']}: {$record['target']}\n";
  }
} else {
  echo "No MX record found for $hostname";
}

Each element here is an associative array with keys such as host, type, pri, and target, so you don't have to juggle two arrays in lockstep. This is the recommended approach for any PHP 8.x project.

  • dns_get_record() — the modern, fully supported way to read any DNS record type, including MX.
  • dns_get_mx() — the procedural alias of getmxrr(); same behavior, same deprecation.
  • checkdnsrr() — check whether DNS records of a given type exist for a host.
  • gethostbyname() — resolve a host name to its IPv4 A record.

Conclusion

The getmxrr() function is a legacy tool for retrieving MX records in older PHP versions. By understanding its syntax and behavior, you can maintain compatibility with legacy codebases. For new projects, we recommend using dns_get_record() with the DNS_MX type flag instead. We hope this article has been informative for working with historical PHP DNS functions.

Practice

Practice
What is the role of the getmxrr() function in PHP?
What is the role of the getmxrr() function in PHP?
Was this page helpful?