Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Network dns_get_mx() Function



The PHP Network dns_get_mx() function is used to find the MX (Mail Exchange) records for a domain. MX records are used to identify a domain's mail servers. This function is similar to the getmxrr() function. It helps identify the mail servers that handle emails for a domain.

The function receives the domain name and returns the names of the MX servers along with their priority. It allows optional weight information to be available to the servers. If records are found, the method returns TRUE; if not, it returns FALSE. It works with PHP 5.0.0 and later versions.

Syntax

Below is the syntax of the PHP Network dns_get_mx() function −

bool dns_get_mx( $host, $mxHosts, $weight );

Parameters

Here are the parameters of the dns_get_mx() function −

  • $host − (Required) It defines the host name for which MX records are to be found.

  • $mxHosts − (Required) It is an array of MX host names discovered.

  • $weight − (Optional) It is an array containing weight information gathered.

Return Value

The dns_get_mx() function returns TRUE if records are found. And FALSE if not found.

PHP Version

First introduced in core PHP 5, the dns_get_mx() function continues to function easily in PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP Network dns_get_mx() function to retrieve the MX records of a given domain and print them.

<?php
   // Fetch and print MX records for a domain
   $domain = "example.com";
   if (dns_get_mx($domain, $mxhosts)) {
      // Print MX hostnames
      print_r($mxhosts); 
   } else {
      echo "No MX records found.";
   }
?>

Output

Here is the outcome of the following code −

Array
(
   [0] => alt2.aspmx.l.google.com
   [1] => alt1.aspmx.l.google.com
   [2] => aspmx.l.google.com
   [3] => alt4.aspmx.l.google.com
   [4] => alt3.aspmx.l.google.com
)

Example 2

In the below PHP code we will try to use the dns_get_mx() function and fetch MX records along with their weights and displays both.

<?php
   // Fetch and display MX records with their weights
   $domain = "example.com";
   if (dns_get_mx($domain, $mxhosts, $weights)) {
      foreach ($mxhosts as $key => $host) {
         echo "Host: $host, Weight: {$weights[$key]}\n";
      }
   } else {
      echo "No MX records found.";
   }
?> 

Output

This will generate the below output −

Host: alt2.aspmx.l.google.com, Weight: 5
Host: alt1.aspmx.l.google.com, Weight: 5
Host: aspmx.l.google.com, Weight: 1
Host: alt4.aspmx.l.google.com, Weight: 10
Host: alt3.aspmx.l.google.com, Weight: 10

Example 3

This below program uses the dns_get_mx() function and checks that a domain has valid MX records and confirms email handling is configured or not.

<?php
   // Validate email configuration of a domain
   $domain = "example.com";
   if (dns_get_mx($domain, $mxhosts)) {
      echo "The domain has valid email servers:\n";
      print_r($mxhosts);
   } else {
      echo "Email is not configured for this domain.";
   }
?> 

Output

This will create the below output −

The domain has valid email servers:
Array
(
   [0] => alt2.aspmx.l.google.com
   [1] => alt1.aspmx.l.google.com
   [2] => aspmx.l.google.com
   [3] => alt4.aspmx.l.google.com
   [4] => alt3.aspmx.l.google.com
)
php_function_reference.htm
Advertisements