使用PHP验证电子邮件地址

时间:2014-06-06 16:03:13

标签: php email-validation

我无法弄清楚我的生活在这里出了什么问题。我注意到表单没有验证电子邮件地址,所以我更改了字符串以更好地匹配正在使用的电子邮件地址,但现在它没有验证任何内容。

以下是表格:

<form id="emailform" method="post" name="emailform" action="formconfirm.php" >
<fieldset>
<legend>Contact Form</legend>
<p><label for="first_name"><strong>* First Name:</strong></label>
<input type="text" name="first_name" id="first_name" maxlength="50" size="40"/></p>
<p><label for="business_name"><strong>* Business Name:</strong></label>
<input type="text" name="business_name" id="business_name" maxlength="50" size="35"/></p>
<p><label for="email_from"><strong>* Email:</strong></label>
<input type="text" name="email_from" id="email_from" maxlength="50" size="47"/></p>
<p><label for="telephone"><strong>Telephone:</strong></label>
<input type="text" name="telephone" id="telephone" maxlength="12" size="43"/></p>
<p><label for="contactmethod"><strong>Preferred Contact Method:</strong></label>
<select name="contactmethod" id="contactmethod">
<option value="select one">- Select One -</option>
<option value="e-mail">E-mail</option>
<option value="phone">Phone</option>
<option value="sms text">Text Message</option>
</select></p>
<p><label for="comments"><strong>Questions, Comments, Suggestions:</strong></label><br />
<textarea name="comments" id="comments" cols="43" rows="4">Thank you for visiting TLTGriffin Designs!</textarea></p>
<p><input name="submit" type="submit" value="Submit" class="buttons" />
<input name="reset" type="reset" value="Reset" class="buttons" /></p>
</fieldset></form>

这是.php:     

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "tltgriffin@gmail.com, tltgriffin@live.com";
$email_subject = "Contact Form Results";

function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted.";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();}

// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['business_name']) ||
!isset($_POST['email_from']) ||
!isset($_POST['telephone']) ||
!isset($_POST['contactmethod']) ||
!isset($_POST['comments'])) {

died('We are sorry, but there appears to be a problem with the form you submitted.');      }

$first_name = $_POST['first_name']; // required
$last_name = $_POST['business_name']; // required
$email_from = $_POST['email_from']; // required
$telephone = $_POST['telephone']; // not required
$contactmethod = $_POST['contactmethod']; // not required
$comments = $_POST['comments']; // not required

$error_message = "";

$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';}
if(!preg_match($string_exp,$business_name)) {
$error_message .= 'The Business Name you entered does not appear to be valid.<br />';}
$email_exp = "/^[A-Za-z0-9._-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/";
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';}

if(strlen($error_message) > 0) {
died($error_message);}

$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Business Name: ".clean_string($business_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Contact Method: ".clean_string($contactmethod)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);}
?>

1 个答案:

答案 0 :(得分:1)

如果您想要符合标准,请参阅我的电子邮件验证功能。

https://gist.github.com/chazmead/8240317

filter_var($email, FILTER_VALIDATE_EMAIL);实际上并不十分一致。