PHP dns_get_mx() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, dns_get_mx() is an alias of getmxrr(). It fills an array with MX host names for a domain and optionally fills a matching array of priorities (weights).

It returns true when MX records are found. Prefer getmxrr() in new code; both names share the same behavior and reference parameters.

Syntax

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

PHP
$ok = dns_get_mx("example.com", $mxhosts, $weights);
CallWhat it does
dns_get_mx("example.com", $mxhosts)Fills $mxhosts with MX server names if any exist.
dns_get_mx("example.com", $mxhosts, $weights)Also fills $weights with matching priorities.
dns_get_mx("example.com", $hosts) ? $hosts[0] : ""Returns the first MX host name when the lookup succeeds.

dns_get_mx() is an alias of getmxrr(). The $hosts and $weights arguments are passed by reference. Lower weight numbers usually mean higher mail priority.

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

Parameters

ParameterDescription
hostnameRequired. The Internet host name to query for MX records.
hostsRequired. A variable passed by reference that receives the list of MX host names.
weightsOptional. A variable passed by reference that receives the priority (weight) for each MX host.

Return Value

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

PHP Examples

Example 1 — List MX Hosts with the Alias

Call dns_get_mx() and print the hosts array. The output is a typical fixed sample.

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

How It Works

  1. dns_get_mx() queries MX records for example. com.
  2. On success it fills $mxhosts by reference.
  3. The loop prints each mail host name.
  4. The two lines shown are typical sample values for teaching.

Example 2 — Hosts and Weights Together

Fill both arrays and print host plus priority side by side.

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

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. Index 0 in $hosts matches index 0 in $weights.
  3. Lower numbers are usually tried first by mail servers.
  4. The printed pairs are a realistic fixed demo.

Example 3 — Same Result as getmxrr()

Show that the alias and getmxrr() fill matching host lists for the same domain.

PHP
<?php
dns_get_mx("example.com", $a);
getmxrr("example.com", $b);

echo count($a), " / ", count($b), PHP_EOL;
echo $a[0] ?? "(none)";

How It Works

  1. Both calls look up the same domain.
  2. Each fills its own array by reference.
  3. The counts match because the functions are aliases.
  4. The first host name is shown from the sample list.

Frequently Asked Questions

dns_get_mx() is an alias of getmxrr(). It loads MX host names (and optional priorities) for a domain into arrays you pass by reference.
No. They are the same function under two names. Prefer getmxrr() in new tutorials and code.
When provided, it receives the MX priority for each host. Lower values usually mean higher priority.
PHP writes the list of MX servers into your variable. You declare $hosts first (or let PHP create it) and read it after a successful call.
No. It only lists mail exchangers for a domain. It does not check whether a specific mailbox exists.

Next: dns_get_record()

dns_get_record() fetches DNS resource records for a hostname and returns them as an array .

dns_get_record() 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