Search code examples
phpmysqlemailverification

Validate Email format PHP


This is a snippet of a user invite code. The code should get the email address from the previous POST form and then check whether it is in the correct format. I used a function called checkMail to do the work. However, it seems like whenever the email is in the wrong format or when the form was left blank it never shows this line:

else if(checkEmail($_POST['email'])==false){ 'Whoops! Looks like the email address you selected is invalid :('; }

or

else { 'Whoops! It looks like you didn't actually add an email address...'; }

Have been stuck for hours, trying to debug... hopefully someone here can help me out!!

Here is the full code:

if(!empty($_POST['email'])) {
        if(checkEmail($_POST['email'])==true) {
            $thisDomain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
            $mailcont = "Someone has invited you to an invite only website!\nYour invite code is: ".$_POST['code'].".\n\nYou can use it at http://".$thisDomain."?go=register&hash=".$_POST['code'];
            if(sendEmail($_POST['email'],'You have been invited!',$mailcont,'noreply@'.$thisDomain)) {
                echo 'Your invite was dispatched to '.$_POST['email'].'<br /><br />Go back <a href="?go=home">home</a>';
            } else { echo 'Whoops! Something went horribly wrong, and we couldn&#39;t send the email :('; }
        } else if(checkEmail($_POST['email'])==false){ 'Whoops! Looks like the email address you selected is invalid :('; }
    } else { 'Whoops! It looks like you didn&#39;t actually add an email address...'; }
break;

Here is the code for the function checkMail:

function checkEmail($email) 
   /**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email 
address format and the domain exists.
*/
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || 
 checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

Thanks in advance guys. You guys are awesome, I am new to stackoverflow but I find myself coming back here everyday for ideas and for the great help from the community!


Solution

  • else { 'Whoops! It looks like you didn&#39;t actually add an email address...'; } should be else { echo 'Whoops! It looks like you didn&#39;t actually add an email address...'; }