代码或服务器问题 - PHP联系表单?

时间:2015-08-03 17:33:28

标签: php

我正在重新设计一个网站,并且刚刚将文件上传到主机,我现在正在测试一切是否正常。消息表单不一致 - 有时它会将用户发送到感谢确认页面,有时会显示错误:" 500内部服务器错误,服务器遇到内部错误或配置错误,无法完成您的请求"但是,没有测试电子邮件到达收件人地址,表明该表单已有效。奇怪的是,当我在mailer.php文档中替换我的个人电子邮件地址时,它成功完成了。这让我觉得代码有效。

我怀疑这是服务器或电子邮件配置问题,因为我在更改主机后也遇到了电子邮件问题,但可能与我的代码有关?

HTML:

<form class="form-horizontal" method="post" action="mailer.php" class="form-horizontal" role="form">
 <div class="form-group">
    <label for="name" class="col-sm-2"><small>Name</small></label>
    <div class="col-sm-10">
       <input type="text" class="form-control" id="inputName" name="inputName" placeholder="Enter your full name" required>
    </div>
</div>
<div class="form-group">
    <label for="email" class="col-sm-2"><small>Email</small></label>
    <div class="col-sm-10">
        <input type="email" class="form-control" id="inputEmail" name="inputEmail" placeholder="Enter your email, example@domain.com" required>
    </div>
</div>
<div class="form-group">
    <label for="name" class="col-sm-2"><small>Telephone</small></label>
        <div class="col-sm-10">
           <input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Enter your telephone number" required>
        </div>
</div>
<div class="form-group">
    <label for="message" class="col-sm-2"><small>Message</small></label>
    <div class="col-sm-10">
        <textarea class="form-control" rows="4" name="inputMessage" id="inputMessage" placeholder="Enter your message here" required></textarea>
    </div>
</div>
<div class="form-group">
<!-- The following field is for robots only, invisible to humans: -->
    <p class="robotic" id="pot">
        <label>If you're human leave this blank:</label>
            <input name="robotest" type="text" name="robotest" id="robotest" class="robotest" />
    </p>
</div>
<div class="form-group">
    <div class="col-sm-10 col-sm-offset-2">
        <input style="font-size:22px;" id="submit" name="submit" type="submit" value="Send" class="btn">
    </div>
</div>
<div class="form-group">
    <div class="col-sm-10 col-sm-offset-2">
    <! Will be used to display an alert to the user>
</div>
</div>
</form>

mailer.php文件:

<?php
/* Set e-mail recipient */
$myemail = "info@domain.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your name was not entered correctly.");
$email = check_input($_POST['inputEmail'], "Your email address was not entered correctly.");
$phone = check_input($_POST['inputPhone'], "Your telephone number was not entered correctly.");
$message = check_input($_POST['inputMessage'], "Your message was not entered correctly.");
$robotest = $_POST['robotest'];

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address.");
}

/* ROBOT TEST */
if($robotest)
{
show_error("Denied, robot.");
}

/*  prepare the message for the e-mail */

$subject = "Inquiry from Website";

$message = "

Someone has sent you a message using your website's contact form:

Name: $name
Email: $email
Telephone: $phone
Subject: $subject

Message:
$message

";

/* send the message using mail() function */
mail($myemail, $subject, $message);

/* redirect visitor to the thank you page */
header('Location: http://www.website.com/thankyou.html');
exit();

/* functions 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>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again.</p>

</body>
</html>
<?php
exit();
}
?>

1 个答案:

答案 0 :(得分:1)

看来你有服务器/配置问题。我已经使用过这段代码并把它放在我自己的服务器上,它运行得很好。 (还有,干得好,代码很干净)

在检查您是否收到电子邮件时要考虑的一件事(特别是当个人电子邮件有效,但公司没有收到时)是电子邮件启发式。尝试更改内容设置和/或主题行。 (主题行对我来说很可疑,我从那里开始)。

https://en.wikipedia.org/wiki/Naive_Bayes_spam_filtering