Example 1 — List MX Hosts
Fetch MX records and print each mail host. Sample hosts are fixed for the tutorial.
<?php
if (getmxrr("example.com", $mxhosts)) {
foreach ($mxhosts as $mx) {
echo $mx, PHP_EOL;
}
} else {
echo "No MX records", PHP_EOL;
} How It Works
- getmxrr() queries DNS for MX records.
- On success it fills $mxhosts by reference.
- The loop prints every mail exchanger name.
- The two names shown are realistic sample values.

