PHP dns_get_mx function tutorial shows how to look up MX records in PHP. Learn dns_get_mx with practical examples for email server validation and configuration.
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.
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.
This example demonstrates the simplest usage of dns_get_mx to get MX records for a domain.
basic_mx_lookup.php
<?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.
This example shows how to display both MX hosts and their priority weights.
mx_with_weights.php
<?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.
This example creates a function to validate if a domain can receive email by checking MX records.
email_domain_validator.php
<?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.
This example demonstrates sorting MX records by their priority weights for proper mail server selection.
sorted_mx_records.php
<?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.
This example checks MX records for multiple domains in one operation.
multiple_domains_check.php
<?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.
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
This tutorial covered the PHP dns_get_mx function with practical examples for MX record lookup and email domain validation scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.