How to validate email address by domain using PHP?

I want to validate the email domain using PHP, because some users trying to submit contact form using dummy email ids like: aa@cc.com for the hacking the website or application for multiple time.

Validate email address by DNS Checker using PHP?

In this tutorial we will learn How to validate email address by domain using PHP? There is a huge difference between checking if the domain name or email address is valid or syntactically correct. we are using to Email Validator library to check email validation. This library is checked the email is exist or email DNS is created or not this is the very helpful library to used in your website.

Install Validate Email address Library and Setup

Download validate email address library from https://github.com/egulias/EmailValidator and include it in your PHP project. You can install validate email address using a composer command. Run the command below to install via Composer.

composer require egulias/email-validator

Available validations

  1. RFCValidation: Standard RFC-like email validation.
  2. NoRFCWarningsValidation: RFC-like validation that will fail when warnings* are found.
  3. DNSCheckValidation: Will check if there are DNS records that signal that the server accepts emails. This does not entail that the email exists.
  4. MultipleValidationWithAnd: It is a validation that operates over other validations performing a logical and (&&) over the result of each validation.
  5. MessageIDValidation: Follows RFC2822 for message-id to validate that field, that has some differences in the domain part.
  6. Your own validation: You can extend the library behaviour by implementing your own validations.

Create HTML Form using Bootstrap & Validate Email with DNS Checker.

Create html form using bootstrap and validate email address using email validation library in PHP.

<?php
	
	// Include autoload 
	include_once("vendor/autoload.php");

	use Egulias\EmailValidator\EmailValidator;
	use Egulias\EmailValidator\Validation\DNSCheckValidation;
	use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
	use Egulias\EmailValidator\Validation\RFCValidation;

	if (isset($_POST['submit'])) {

		//Get email Value from post form
		$email = $_POST['email'];

		$validator = new EmailValidator();
		$multipleValidations = new MultipleValidationWithAnd([
		    new RFCValidation(),
		    new DNSCheckValidation()
		]);

		$result = $validator->isValid($email, $multipleValidations); //true
		
		if ($result) {
			$success = "Email is validated";
		}else{
			$error = "Email is not validated";
		}
	}
?>

<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Perfect way to Validate Email ID using PHP | PHP Email ID DNS Validation</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
	  	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
	  	<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.slim.min.js"></script>
	  	<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
	  	<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
    </head>
<body>
    <div class="container card p-5">
        <div class="py-5 text-center">
            <h2>Perfect way to Validate Email ID using PHP | PHP Email ID DNS Validation</h2>
        </div>
        <div class="container d-flex justify-content-center">
        	<div class="col-md-6">
        		<?php
			        if (!empty($error)) {
			            echo "<div class='alert alert-danger alert-dismissible'>
			                    <button type='button' class='close' data-dismiss='alert'>×</button>$error
			                  </div>";
			        } else if (!empty($success)) {
			            echo "<div class='alert alert-success alert-dismissible'>
			                    <button type='button' class='close' data-dismiss='alert'>×</button>$success
			                  </div>";
			        }
		      	?>
	            <form action="" method="POST">
				  	<div class="form-group">
				    	<label for="email">Email address:</label>
				    	<input type="text" name="email" class="form-control" placeholder="Enter email" required>
				  	</div>
				  <button type="submit" name="submit" class="btn btn-primary">Submit</button>
				</form>
			</div>
        </div>
    </div>
</body>
</html>

See also

  1. How to Send Email from localhost in PHP using SMTP
  2. Send Multiple Emails at once in Gmail using PHP jQuery AJAX
  3. How to Send Bulk Emails in PHP using PHPMailer with Ajax jQuery
  4. Contact form with validation in PHP mailer jquery,AJAX in PHP
  5. How to configure SMTP server to Send Email using PHP?

Conclusion

In this article, I have explain the process of How to Validate email address by domain in PHP. I have tried to very simple way to validate email address by domain or DNS checker in PHP. You can easily extend the functionality according to your needs. To get all the necessary files, including the EmailValidator library.

You can always support by sharing on social media or recommending my blog to your friends and colleagues. If you have any suggestions or problems about this tutorial, please comment on the form below.😊

Leave a Reply

Your email address will not be published. Required fields are marked *