you can try like below as a example
<?php
function domain_exists($email, $record = 'MX'){
list($user, $domain) = explode('@', $email);
return checkdnsrr($domain, $record);
}
if(isset($_POST['email'])){
$email = $_POST['email'];
if(domain_exists($email)) {
echo('This MX records exists; I will accept this email as valid.');
}
else {
echo('No MX record exists; Invalid email.');
}
}
?>
But, this isn't reliable. dns isn't reliable in general. It's best-effort. It can also take a long time when an invalid domain is provided, so you need to consider a potential DOS of your system if someone hammers your script with a lot of invalid/offline domains.
In (little known) fact, domains don't even need to have an MX record for mail to be delivered. Mail delivery is supposed to fall back to using the A record for the host specified. That may not really happen much these days, but in theory..
so you can validate your email with preg match like
//$email = "abc123@lolhaha"; // Invalid email address
$email = "somebody@somesite.com"; // Valid email address
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if (preg_match($regex, $email)) {
echo $email . " is a valid email. We can accept it.";
} else {
echo $email . " is an invalid email. Please try again.";
}