Current Location: Home> Latest Articles> How to Use the dns_check_record Function to Verify MX Records: A Step-by-Step Guide

How to Use the dns_check_record Function to Verify MX Records: A Step-by-Step Guide

gitbox 2025-07-09

How to Use the dns_check_record Function to Verify MX Records: A Step-by-Step Guide

In PHP, the dns_check_record function can be used to check for specific DNS record types, especially MX (Mail Exchange) records. MX records are DNS entries that designate which mail servers are responsible for receiving emails. If you need to verify whether a domain has MX records set up, dns_check_record is a highly effective tool.

What is an MX Record?

An MX record is a type of DNS (Domain Name System) entry used to specify the mail server responsible for accepting and forwarding emails. Every mail server needs an MX record pointing to its IP address. Without an MX record, emails cannot be correctly routed to the mail server.

Using the dns_check_record Function

The dns_check_record function checks whether a specific type of DNS record exists for a given domain. It supports multiple record types, including A, MX, and CNAME records. In this guide, we’ll focus specifically on checking for MX records.

Syntax of the dns_check_record Function

<span><span><span class="hljs-title function_ invoke__">dns_check_record</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$hostname</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$type</span></span><span> = DNS_MX): </span><span><span class="hljs-keyword">bool</span></span><span>
</span></span>
  • $hostname: The domain name to be checked (e.g., example.com).

  • $type: The record type to check for, with the default being DNS_MX (checks for MX records). To check for other types, use the corresponding constant (e.g., DNS_A, DNS_CNAME).

  • Return value: Returns true if the specified record exists, or false if it doesn’t.

Step-by-Step Usage

  1. Basic MX Record Check

    If you just want to verify whether a domain has MX records, the simplest way is to call dns_check_record and pass in the domain name.

    <span><span><span class="hljs-meta">&lt;?php</span></span><span>
    </span><span><span class="hljs-variable">$domain</span></span><span> = </span><span><span class="hljs-string">"example.com"</span></span><span>;
    </span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">dns_check_record</span></span><span>(</span><span><span class="hljs-variable">$domain</span></span><span>, DNS_MX)) {
        </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"<span class="hljs-subst">$domain</span></span></span><span> has MX records.";
    } </span><span><span class="hljs-keyword">else</span></span><span> {
        </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"<span class="hljs-subst">$domain</span></span></span><span> does not have MX records.";
    }
    </span><span><span class="hljs-meta">?&gt;</span></span><span>
    </span></span>

    This simple script outputs whether the specified domain has MX records.

  2. Retrieving Detailed MX Record Information

    If you also want to fetch the actual MX record values and their priorities, use the getmxrr function. It retrieves all MX hosts and their corresponding weights.

    ... (same code structure retained, omitted here for brevity) ...

    In this example, we use getmxrr to get the list of MX server addresses ($mxhosts) and their weights ($mxweights). This is useful for a more in-depth understanding of the domain’s email configuration.

  3. Checking MX Records for Multiple Domains

    If you need to check multiple domains, you can loop through an array of domain names:

    ... (same code structure retained, omitted here for brevity) ...

    This allows you to check several domains in one go.

Things to Note

  1. DNS Caching: Sometimes DNS query results may be affected by local or DNS server caching, which could lead to outdated MX record data. If this happens, try flushing the cache or using an alternate DNS resolver.

  2. IPv6 Support: Many mail servers now support IPv6. If you need to check for IPv6 addresses, use the DNS_AAAA type with dns_check_record.

  3. MX Record Priority: MX records typically include a priority value (weight). Lower numbers indicate higher priority. Understanding this helps you grasp how email servers are prioritized.

Conclusion

Using the dns_check_record function in PHP makes it easy to determine whether a domain has configured MX records for email. This is crucial for verifying email system setups. Combined with the getmxrr function, you can obtain detailed MX record data, giving you a complete picture of a domain’s mail server configuration. These tools are invaluable for ensuring the stability and reliability of email services during development.