Email_verification.php
DZone's Guide to
Email_verification.php
Join the DZone community and get the full member experience.
Join For Free
$email = 'test@domain.com';
if(verify_email($email)){
// E-mail address looks to be in the proper format
// lets check the MX records
if(verify_email_dns($email)){
// E-mail passed both checks
echo 'Success - E-mail address appears to be valid.';
} else {
// E-mail is invalid, no MC record
echo 'Error - E-mail domain does not have an MX record.';
}
} else {
// E-mail inst formatted correctly
// so we dont even check its MX record
echo 'Error - E-mail address appears to be invalid.';
}
// Our function to filter our bogus formatted addresses
function verify_email($email){
if(!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email)){
return false;
} else {
return $email;
}
}
// Our function to verify the MX records
function verify_email_dns($email){
// This will split the email into its front
// and back (the domain) portions
list($name, $domain) = split('@',$email);
if(!checkdnsrr($domain,'MX')){
// No MX record found
return false;
} else {
// MX record found, return email
return $email;
}
}
Topics:
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}