PHP dns_get_mx Function
last modified April 4, 2025
The PHP dns_get_mx
function retrieves MX (Mail Exchange) records
for a given host. It's essential for email server configuration and validation.
Basic Definition
dns_get_mx
gets the MX records for a specified internet hostname.
MX records identify mail servers responsible for accepting email messages.
Syntax: dns_get_mx(string $hostname, array &$hosts, array &$weights = null): bool
.
Returns true on success, false on failure. Populates arrays with hosts and weights.
Basic MX Record Lookup
This example demonstrates the simplest usage of dns_get_mx
to get
MX records for a domain.
<?php declare(strict_types=1); $domain = "example.com"; $mxhosts = []; $mxweights = []; if (dns_get_mx($domain, $mxhosts, $mxweights)) { echo "MX records for $domain:\n"; print_r($mxhosts); } else { echo "No MX records found for $domain"; }
This code checks for MX records and prints the mail servers if found.
The function populates the $mxhosts
array with server names.
Displaying MX Records with Priorities
This example shows how to display both MX hosts and their priority weights.
<?php declare(strict_types=1); $domain = "gmail.com"; $mxhosts = []; $mxweights = []; if (dns_get_mx($domain, $mxhosts, $mxweights)) { echo "MX records for $domain:\n"; foreach ($mxhosts as $key => $host) { echo "Host: $host, Priority: {$mxweights[$key]}\n"; } } else { echo "No MX records found for $domain"; }
MX records have priorities (weights) that determine the order of mail server usage. Lower numbers indicate higher priority in email delivery.
Email Domain Validation
This example creates a function to validate if a domain can receive email by checking MX records.
<?php declare(strict_types=1); function isValidEmailDomain($email): bool { $parts = explode('@', $email); if (count($parts) != 2) return false; $domain = $parts[1]; $mxhosts = []; return dns_get_mx($domain, $mxhosts); } $email = "user@example.com"; echo isValidEmailDomain($email) ? "Valid domain" : "Invalid domain";
This function splits the email address to extract the domain part. It then checks if the domain has configured MX records for receiving email.
Sorting MX Records by Priority
This example demonstrates sorting MX records by their priority weights for proper mail server selection.
<?php declare(strict_types=1); $domain = "yahoo.com"; $mxhosts = []; $mxweights = []; if (dns_get_mx($domain, $mxhosts, $mxweights)) { array_multisort($mxweights, $mxhosts); echo "Sorted MX records for $domain:\n"; foreach ($mxhosts as $key => $host) { echo "{$mxweights[$key]}: $host\n"; } } else { echo "No MX records found for $domain"; }
Mail servers should be contacted in priority order. This code uses
array_multisort
to sort hosts by their weights.
Checking Multiple Domains
This example checks MX records for multiple domains in one operation.
<?php declare(strict_types=1); $domains = ["google.com", "microsoft.com", "example.com"]; foreach ($domains as $domain) { $mxhosts = []; if (dns_get_mx($domain, $mxhosts)) { echo "$domain has ".count($mxhosts)." MX records\n"; } else { echo "$domain has no MX records\n"; } }
This batch processing approach is useful when you need to validate multiple email domains or check mail server configurations for several domains.
Best Practices
- Caching: Cache MX lookups to reduce DNS queries
- Error Handling: Handle DNS lookup failures gracefully
- Timeouts: Consider setting timeout for DNS queries
- Validation: Combine with other email validation methods
- Security: Sanitize input to prevent DNS poisoning
Source
This tutorial covered the PHP dns_get_mx
function with practical
examples for MX record lookup and email domain validation scenarios.