The dns_get_mx() function is an inbuilt function in PHP which returns MX records for the specified internet host name. This function is an alias of the getmxrr() function.
Syntax:
bool dns_get_mx( $host, $mxhosts, $weight );
Parameter: This function accepts three parameters as mentioned above and described below:
- $host: It is required parameter. It specifies the host name whose MX records to be found.
- $mxhosts: It is required parameter. An array specifies MX host names found.
- $weight: It is optional parameter. An array filled with weight information gathered.
Return Value: This function returns TRUE if any record(s) found, otherwise returns FALSE.
Note: This function is available for PHP 5.0.0 and newer version.
Below programs illustrate the dns_get_mx() function in PHP:
Program 1:
<?php $domain = "geeksforgeeks.org"; if(dns_get_mx($domain, $mx_details)) { foreach( $mx_details as $key => $value) { echo "$key => $value <br>"; } } ?> |
Output:
0 => alt3.aspmx.l.google.com 1 => alt4.aspmx.l.google.com 2 => aspmx.l.google.com 3 => alt2.aspmx.l.google.com 4 => alt1.aspmx.l.google.com
Program 2:
<?php $domain = "yahoo.com"; if(dns_get_mx($domain, $mx_details)) { foreach( $mx_details as $key => $value ) { echo "$key => $value <br>"; } } ?> |
Output:
0 => mta5.am0.yahoodns.net 1 => mta6.am0.yahoodns.net 2 => mta7.am0.yahoodns.net
Reference: https://www.php.net/manual/en/function.dns-get-mx.php
Recommended Posts:
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

