The Code Developer

Email Address Validation In PHP

Simple


$email = 'mail@example.com';
$validation = filter_var($email, FILTER_VALIDATE_EMAIL);

if ( $validation ) {
 $output = 'Correct email address.';
 echo $output;
}
else {
 $output = 'Sorry! wrong email address.';
 echo $output;
}

Advanced

This function check email address format is correct, it also performs a test if the host is existing.

<?php
$email="test@geemail.com";
if (isValidEmail($email))
{
  echo "Correct email address.";
}
else
{
   echo "Sorry! wrong email address.";
}

//Check-Function
function isValidEmail($email)
{
   //Perform a basic syntax-Check
   //If this check fails, there's no need to continue
   if(!filter_var($email, FILTER_VALIDATE_EMAIL))
   {
		   return false;
   }

   //extract host
   list($user, $host) = explode("@", $email);
   //check, if host is accessible
   if (!checkdnsrr($host, "MX") && !checkdnsrr($host, "A"))
   {
		   return false;
   }

   return true;
}
?>