diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 54bbbc837a1..51e64c22878 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -2782,6 +2782,35 @@ function isValidEmail($address, $acceptsupervisorkey=0) return false; } +/** + * Return if the domain name has a valid MX record. + * WARNING: This need function idn_to_ascii, checkdnsrr and getmxrr + * + * @param string $domain Domain name (Ex: "yahoo.com", "yhaoo.com", "dolibarr.fr") + * @return int -1 if error (function not available), 0=Not valid, 1=Valid + */ +function isValidMXRecord($domain) +{ + if (function_exists('idn_to_ascii') && function_exists('checkdnsrr')) + { + if (! checkdnsrr(idn_to_ascii($domain), 'MX')) + { + return 0; + } + if (function_exists('getmxrr')) + { + $mxhosts=array(); + $weight=array(); + getmxrr(idn_to_ascii($domain), $mxhosts, $weight); + if (count($mxhosts) > 1) return 1; + if (count($mxhosts) == 1 && ! empty($mxhosts[0])) return 1; + + return 0; + } + } + return -1; +} + /** * Return true if phone number syntax is ok * TODO Decide what to do with this diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index 71b39fc6e51..c72daec989e 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -122,6 +122,31 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase + /** + * testIsValidMXRecord + * + * @return void + */ + public function testIsValidMXRecord() + { + // Nb of line is same than entry text + + $input="yahoo.com"; + $result=isValidMXRecord($input); + print __METHOD__." result=".$result."\n"; + $this->assertEquals(1, $result); + + $input="yhaoo.com"; + $result=isValidMXRecord($input); + print __METHOD__." result=".$result."\n"; + $this->assertEquals(0, $result); + + $input="dolibarr.fr"; + $result=isValidMXRecord($input); + print __METHOD__." result=".$result."\n"; + $this->assertEquals(0, $result); + } + /** * testDolGetFirstLineOfText *