【发布时间】:2014-10-24 22:12:50
【问题描述】:
我的脚本有问题没有发送电子邮件,返回错误消息是:电子邮件地址无效!当我在文本字段中输入我的电子邮件地址时,就会发生这种情况。我有一种感觉是(preg_match)方法造成了这个问题,但是在网上看了之后我并没有真正理解该方法的内容。希望大家帮忙,谢谢。
源代码:
<?php
/*Select email recipient*/
$myemail = "info@shadowempires.url.ph";
/*Check all form inputs using check input function*/
$name = check_input($_POST['name'], "Please enter your name");
$email = check_input($_POST['email'], "Please enter your email address.");
$comment = check_input($_POST['comment'], "Please write a message.");
/*If email is not valid show error message*/
if (!preg_match("/(\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){
show_error("Email address not valid!");
}
/*Lets prepare the message for the email*/
$message = "Customer Question!
Contact form has been submitted by:
Name: $name
Email: $email
Comments: $comment
End of message";
/*Send the message using mail() function*/
mail($myemail, $message);
/*Redirect visitor to the thank you page*/
header('Location: thankyou.htm');
exit();
/*Functions we used*/
function check_input($data, $problem=''){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0){
show_error($problem);
}
return $data;
}
function show_error($myError){
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php exit();
}
?>
【问题讨论】:
标签: php forms validation email