-2

I want to validate the e-mail domain using php, because some users trying to submit contact form using dummy email ids like: aa@bb.com

Sanjay
  • 370
  • 4
  • 9
  • 2
    Cool. Go have a go, then come back to us. – Farkie Jun 01 '17 at 06:49
  • Validate how? Matching it against a blacklist? Checking if the address exists? If it is of valid format? – Ivar Jun 01 '17 at 06:50
  • There is a huge difference between checking if the domainname (and/or email address) is valid, or syntactically correct. The syntax you can check with regex, but that will tell you that above email address is actually correct. Checking if it is valid is best done by e.g. sending a confirmation email to the address with a link to click. So best to check syntax before, then doing a validation with a confirmation email after. – Doqnach Jun 01 '17 at 06:51
  • search for `PHP validate email regex` in Google or Stackoverflow. This question has been answered many times. – SacrumDeus Jun 01 '17 at 06:53
  • @SacrumDeus as per regex above mentioned email id is valid but domain name 'bb.com' not valid so am looking for domain validation, there is any solution – Sanjay Jun 01 '17 at 07:01
  • @sanjay even for this, there is a solution. Search for `php check if url is reachable` or `php check if domain is reachable` in Google or Stackoverflow. This is a pretty common question to prevent spam and others... – SacrumDeus Jun 01 '17 at 07:51

4 Answers4

2

TRY with checkdnsrr extract the domain name from the email address and pass to the checkdnsrr.

Returns TRUE if domain name are found; returns FALSE if no domain name were found or if an error occurred.

$domainname = "domain.com";

checkdnsrr($domainname , "A");
Narayan
  • 1,670
  • 1
  • 19
  • 37
  • Isn't it better to check the MX record? The fact that the domain exists doesn't mean it can receive email. – Ivar Jun 01 '17 at 07:14
  • @sanjay Glad to help, you can accept the answer if it is worked for you or helpful to you. – Narayan Jun 01 '17 at 07:15
  • @Ivar yes MX is better also there are many option available in type like `A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT ` – Narayan Jun 01 '17 at 07:16
  • Looks like checking for MX is not a solution. From the php docs: " Only the mailexchangers found in DNS are returned, however, according to » RFC 2821 when no mail exchangers are listed, hostname itself should be used as the only mail exchanger with a priority of 0." https://www.php.net/manual/en/function.getmxrr.php – PiTheNumber Jun 07 '23 at 08:37
0

If you only want to validate the syntax of the domain name section you could split the email on the @ and run this regex on the second part (after running it through the idn_to_ascii() function for intenational domain names):

/
    ^
    (?(DEFINE)(?<part>(?:xn--)?[a-z\d](?:[a-z\d-]*[a-z\d])?))
    (?(DEFINE)(?<subpart>(?:xn--)?[a-z\d_](?:[a-z\d_-]*[a-z\d])?))
    (?:(?&subpart)\.)*
    (?&part)
    (?:\.[a-z]+|\.xn--[a-z\d]+){1,2}
    $
/xigm

https://regex101.com/library/PAKVdK

Doqnach
  • 372
  • 1
  • 8
0

You will need to check if there is a MX record for that domain.

Consider this script in addition to you regex validation

https://davidwalsh.name/php-email-validator

Be aware that this will not 'really' validate emails completely. User could be invalid.

-1

If you don't want to go through all the hassle of doing the validations by yourself, just use the free API plan from MailboxValidator.

They have some sample codes to help you with integration.

http://www.mailboxvalidator.com/api-single-validation

<?php
$apiKey = 'Enter_License_Key';
$params['format']           = 'json';
$params['email']     = 'Enter_Email';

$query = '';

foreach($params as $key=>$value){
    $query .= '&' . $key . '=' . rawurlencode($value);
}

$try = 0;
do {
    ////////////
    //For https request, please make sure you have enabled php_openssl.dll extension.
    //
    //How to enable https
    //- Uncomment ;extension=php_openssl.dll by removing the semicolon in your php.ini, and restart the apache.
    //
    //In case you have difficulty to modify the php.ini, you can always make the http request instead of https.
    ////////////
    $result = file_get_contents('https://api.mailboxvalidator.com/v1/validation/single?key=' . $apiKey . $query);
} while(!$result && $rty++ < 3);

$data = json_decode($result);

print_r($data);
?>
Vlam
  • 1,622
  • 1
  • 8
  • 17