So, you’ve created a new user system but you need a way to see if an email address that someone has registered is genuine. This small script will do this by checking the email’s MX records.

<?php
function CheckEmail($email,$record = 'MX'){ //define a function
	list($user,$domain) = split('@',$email); //split up the email into two variables $user & $domain
	if(checkdnsrr($domain,$record)){ //Use PHP function to check MX records
        return $email.': This MX record exists, I will accept this email as valid.';  //true
    }else{ 
        return $email.': No MX record exists, Invalid email.'; //false
    } 
}
 
echo CheckEmail("luke@luke-haynes.co.uk");
echo '<br>';
echo CheckEmail("fake@fakeemaildomain.old");
?>

This code outputs:

luke@luke-haynes.co.uk: This MX record exists, I will accept this email as valid.
fake@fakeemaildomain.old: No MX record exists, Invalid email.

This is a very effective method of checking if an email is legitimate but the only way to fully prove that the email belongs to the user we need to send a confirmation email. (tutorial soon)

Leave a Reply

*