im的脚本有问题,没有发送电子邮件,返回的错误消息是:电子邮件地址无效!当我在文本字段中输入电子邮件地址时,就会发生这种情况。我有一种感觉,是(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();
}
?>
filter_var:怎么样
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// valid email address
}
这是验证电子邮件地址的一种简单方法。
更新1
看看这个答案。这里有更多关于使用regex验证电子邮件地址的信息:如何在PHP 中验证电子邮件地址
更新2
有一个测试正则表达式电子邮件模式的工具,请查看此处
您需要修复regexp,下面是一个工作示例:
/(('w|'-))+'@(('w|'-))+'.(('w|'-))+/