0

我已经设置了一个简单的联系表格,但在最后的润色方面遇到了一些麻烦。每当提交表单时,我都会收到一条错误消息。希望有人可以看看我的代码并指出我的错误。谢谢!

(已编辑)忘记提及错误是,一旦提交表单,我将被重定向到错误页面(error.html)而不是成功(contactthanks.php)页面。

HTML:

<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />

<label for="City">City:</label>
<input type="text" name="City" id="City" />

<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />

<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>

<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>

PHP:

<?php

$EmailFrom = "";
$EmailTo = "MYEMAIL@gmail.com";
$Subject = "";
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 


$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
exit;
}


$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";


$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
4

2 回答 2

1
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}

它正在重定向到您的错误页面,因为调用mail正在返回false(表明电子邮件未被接受传递),而这正是您告诉它在这种情况下执行的操作。

您需要检查服务器上设置的 SMTP。

sendmail_from通常要检查的设置sendmail_path在您的 PHP.ini 文件中。看到您已经设置了From标题,sendmail_path可能没有正确设置。

于 2012-12-17T08:29:02.413 回答
0

查看导致问题的原因

ini_set("display_errors", "1"); 
error_reporting(E_ALL);`. 

我的猜测是你有错误的 smtp 设置和mail功能失败。

于 2012-12-17T08:27:36.823 回答