我的网站上有一张表格:
<form name="contact" action="contact.php" method="post">
<label for="name">Name:</label><br/><input type="text" name="name" id="name"><br/>
<label for="email">Email:</label><br/><input type="text" name="email" id="email"><br/>
<label for="comment">Question:</label><br/><textarea name="comment" id="comment"></textarea><br/>
<input type="submit" value="Send" id="submit">
</form>
这是它将数据提交到的脚本:
<?php
header("Refresh: 3;url=http://www.xyz.com/");
if(isset($_POST['email'])) {
$email_to = "xxxx@gmail.com";
$email_subject = "Enquiry";
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['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comment'])){
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$comment = $_POST['comment']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($comment) < 2) {
$error_message .= 'The Comments you entered do 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 .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Comment: ".clean_string($comment)."\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);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
由于某种原因,脚本输出消息已成功发送,但是当我检查我的收件箱时,没有新的电子邮件。我不知道发生了什么,有人可以帮助我吗?
答案 0 :(得分:3)
邮件功能已关闭错误
@mail($email_to, $email_subject, $email_message, $headers);
从中移除@
并添加简单的
if(mail($email_to, $email_subject, $email_message, $headers))
{
echo 'mail was sent'; //success message here
}
else
{
echo 'there were errors during sending mail'; //error message there
}
当您发现发生了哪些错误时,您可以删除它们。配置SMTP服务器可能会出现问题。
您可以在php.ini中设置一些影响mail()函数的设置
此外,显示此Thank you for contacting us. We will be in touch with you very soon.
消息始终不检查是否出现错误。