php电子邮件地址验证不起作用?

Hey guys, I know there are a lot better ways to send email with PHP. However, for this purpose the easiest solution is perfect. Only thing: I can't find out why, my validation is not working!

I get the email address, however the validation is not working, I'm able to send a completely empty form. Any ideas?

<?php
//Email
$Name = Trim(stripslashes($_POST['author']));
$EmailFrom = Trim(stripslashes($_POST['email']));
$Subject = Trim(stripslashes($_POST['subject']));
$Comment = Trim(stripslashes($_POST['comment']));
$EmailTo = "myemail@address.com";

/*$Name = "Some Name";
$EmailFrom = "test@test.com";
$Subject = "Test";
$Comment = "Why is the validation not working, don't get it?";
$EmailTo = "myemail@address.com";*/


/*echo $Name . " length: " . strlen($Name) . "<br/>";
echo $EmailFrom . " length: " . strlen($EmailFrom) . "<br/>";
echo $Subject . " length: " . strlen($Subject) . "<br/>";
echo $Comment . " length: " . strlen($Comment) . "<br/>";
echo $EmailTo . " length: " . strlen($EmailTo) . "<br/>";*/


//***************************
//Validation
//***************************
$validationOK=true;
if ($Name == "") $validationOK=false;
if (isValidEmail($EmailFrom) == 0) $validationOK=false;
if ($Subject == "") $validationOK=false;
if ($Comment == "") $validationOK=false;
function isValidEmail( $email = null ) {
    return preg_match( "/^[\d\w\/+!=#|$?%{^&}*`'~-][\d\w\/\.+!=#|$?%{^&}*`'~-]*@[A-Z0-9][A-Z0-9.-]{1,61}[A-Z0-9]\.[A-Z]{2,6}$/ix", $email );
}

if (!$validationOK) {
  print "error";
}

//***************************
//Order
//***************************
$Body = "Contactform";
$Body .= "

";
$Body .= $Comment;

// Email Headers with UTF-8 encoding
$email_header = "From: " . $EmailFrom . "
";
$email_header .= "Content-Type: text/plain; charset=UTF-8
";
$email_header .= "Reply-To: " . $EmailFrom . " 
";
// send email 
$success = mail($EmailTo, $Subject, $Body, $email_header);

//***************************
//Success or Error
//***************************
if ($success){
  print "success";
}
else{
  print "error";
}

?>

You're printing the word error if $validationOK is false but you're not halting the script at that point so php continues to process the commands after it.

Try the following

if (!$validationOK) {
  print "error";
  exit();
}

you need to return after the condition

if (!$validationOK) {
  print "error";
  // return or exit
}

dont continue to send mail

You don't end your script after printing "error". You should end the script or make some other change so it won't be sent on error. For example:

if (!$validationOK) {
  print "error";
  exit;
}

This code:

if (!$validationOK) {
  print "error";
}

does not keep your program from sending a mail, it just prints error message.

Don't write your own email validation function when there's tried and tested free software to do it for you.

You are welcome to use my free PHP function is_email() to validate addresses. It's available to download [here][1].

It will ensure that an address is fully RFC 5321 compliant. It can optionally also check whether the domain actually exists and has an MX record.

You shouldn't rely on a validator to tell you whether a user's email address actually exists: some ISPs give out non-compliant addresses to their users, particularly in countries which don't use the Latin alphabet. More in my essay about email validation here: [http://isemail.info/about][2].