I wrote the following function to validate emails for a web app I'm working on:
function validate_email($email) {
$valid = true;
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$valid = false;
}
$email_parts = explode("@", $email);
if(count($email_parts) < 2) {
$valid = false;
}
else {
if(!checkdnsrr($email_parts[1], "MX")) {
$valid = false;
}
}
return $valid;
}
It's not intended to replace the validation email sent to the user with a link to complete the registration. I know it wont catch all invalid ones, it will just reduce them. Hence reduce the amount of bounced emails received. My questions are:
- Will it flag a valid emails as a false positive?
- Is it okay to depend on a DNS check to validate the domain?
Any help is appreciated. Thanks.