PHP getmxrr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, getmxrr() looks up MX (mail exchanger) records for a host name. It fills $hosts with the MX server names and returns true when records are found.

Pass an optional third array by reference to receive matching priority weights. dns_get_mx() is an alias of this function. For richer DNS data, dns_get_record() with DNS_MX is another option.

Syntax

Call getmxrr() with the values you need. Start with this line.

PHP
$ok = getmxrr("example.com", $mxhosts, $weights);
CallWhat it does
getmxrr("example.com", $mxhosts)Fills $mxhosts with MX host names when records exist.
getmxrr("example.com", $mxhosts, $weight)Also fills $weight with priorities for each host.
getmxrr("example.com", $hosts) ? count($hosts) : 0Returns how many MX hosts were found, or 0 on failure.

Both $hosts and $weights are written by reference. After a successful call, $hosts[$i] pairs with $weights[$i]. Lower weights usually mean higher mail priority.

Library declarations
PHP
getmxrr(string $hostname, array &$hosts, array &$weights = null): bool

Parameters

ParameterDescription
hostnameRequired. The Internet host name whose MX records you want.
hostsRequired. Passed by reference; receives the list of MX host names.
weightsOptional. Passed by reference; receives the priority for each MX host.

Return Value

Return ValueDescription
booltrue if any MX records were found; false if none were found or an error occurred.

PHP Examples

Example 1 — List MX Hosts

Fetch MX records and print each mail host. Sample hosts are fixed for the tutorial.

PHP
<?php
if (getmxrr("example.com", $mxhosts)) {
    foreach ($mxhosts as $mx) {
        echo $mx, PHP_EOL;
    }
} else {
    echo "No MX records", PHP_EOL;
}

How It Works

  1. getmxrr() queries DNS for MX records.
  2. On success it fills $mxhosts by reference.
  3. The loop prints every mail exchanger name.
  4. The two names shown are realistic sample values.

Example 2 — Sort by Priority Weight

Fill hosts and weights, then sort so the lowest weight (highest priority) comes first.

PHP
<?php
getmxrr("example.com", $hosts, $weights);

array_multisort($weights, $hosts);

for ($i = 0; $i < count($hosts); $i++) {
    echo $weights[$i], " ", $hosts[$i], PHP_EOL;
}

How It Works

  1. The third argument receives priority numbers.
  2. array_multisort() keeps hosts and weights aligned while sorting.
  3. Lower weights are listed first after sorting.
  4. Printed pairs are fixed demo data.

Example 3 — Pick the Primary MX Host

After a successful lookup, choose the host with the smallest weight.

PHP
<?php
if (!getmxrr("example.com", $hosts, $weights)) {
    echo "none", PHP_EOL;
} else {
    $bestIndex = array_keys($weights, min($weights))[0];
    echo $hosts[$bestIndex], PHP_EOL;
}

How It Works

  1. Exit early when no MX records are found.
  2. min($weights) finds the best (lowest) priority value.
  3. array_keys(. .., min(...))[0] finds one matching index.
  4. We print the host at that index from the sample set.

Frequently Asked Questions

getmxrr() loads MX records for a domain into arrays you pass by reference and returns true when records are found.
It is an optional by-reference array that receives the MX priority for each host in $hosts.
No. dns_get_mx() is an alias of getmxrr().
No. It only lists mail exchangers for the domain part. Mailboxes and acceptance policies are separate.
No MX records were found, or the DNS lookup failed. According to mail standards, the host itself may still accept mail when MX records are absent.

Next: getprotobyname()

getprotobyname() converts an internet protocol name such as "tcp" or "udp" into its protoc.

getprotobyname() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful