Content · Programming · PHP

PHP getmxrr() Function: Usage and Examples

Understand PHP getmxrr() with three examples, return behavior, important pitfalls, and related operations.

2 views Published Oct 16, 2026 Updated Oct 16, 2026

Use getmxrr() when you need to looks up mail exchange records for a domain in a PHP application. The examples below show the actual operation and its limitations.

What Is getmxrr() in PHP?

Looks up mail exchange records for a domain. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

getmxrr(string $hostname, &$hosts, &$weights = ...): bool

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

$mxHosts = []; $result = getmxrr("example.invalid",$mxHosts); echo $result ? "Records found" : "No records";

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Example 2: Different Input or Behavior

<?php

$mailServers = []; var_dump(getmxrr("localhost",$mailServers));

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Practical Example: Applying the Function

<?php

$hosts = []; $weights = []; if (getmxrr("example.invalid",$hosts,$weights)) { foreach ($hosts as $index=>$host) echo $host . ":" . $weights[$index] . PHP_EOL; } else { echo "No MX records"; }

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Important Behavior and Edge Cases

MX records do not prove an email address exists or that a mailbox accepts mail.

Comparison With Related Functions

ApproachDifference
getmxrr()Looks up mail exchange records for a domain.
Related operationdns_get_record() can retrieve MX and other DNS record types.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationMX records do not prove an email address exists or that a mailbox accepts mail.
Confusing this function with a related operationdns_get_record() can retrieve MX and other DNS record types.

Key Takeaways

  • Looks up mail exchange records for a domain.
  • MX records do not prove an email address exists or that a mailbox accepts mail.
  • dns_get_record() can retrieve MX and other DNS record types.

Use getmxrr() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

Share this resourceSend this page to someone who may find it useful.