【问题标题】:Receiving 500 Server Error with Contact Form Submission.php使用联系表单 Submission.php 接收 500 服务器错误
【发布时间】:2020-12-22 05:05:24
【问题描述】:

我查看了一堆类似的问题,但无法找到有效的答案。我最近将我的网站切换到了不同的主机,并正在尝试设置联系表格。单击提交时出现 500 错误。我已确保正确设置了 SMTP。我只是不知道在这一点上还能尝试什么。这是我的代码:

HTML:

 <form role="form" method="POST" action="contact-form-submission.php">
                <div class="row">
                  <div class="form-group col-lg-4">
                    <label for="input1">Name</label>
                    <input type="text" name="contact_name" class="form-control" id="input1">
                  </div>
                  <div class="form-group col-lg-4">
                    <label for="input2">Email Address</label>
                    <input type="email" name="contact_email" class="form-control" id="input2">
                  </div>
                  <div class="form-group col-lg-4">
                    <label for="input3">Phone Number</label>
                    <input type="phone" name="contact_phone" class="form-control" id="input3">
                  </div>
                  <div class="clearfix"></div>
                  <div class="form-group col-lg-12">
                    <label for="input4">Message</label>
                    <textarea name="contact_message" class="form-control" rows="6" id="input4"></textarea>
                  </div>
                  <div class="form-group col-lg-12">
                    <input type="hidden" name="save" value="contact">
                    <button type="submit" class="btn btn-primary">Submit</button>
                  </div>
              </div>
            </form>

PHP:

<?php

// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
    header('Location: contact.php'); exit;
}

// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];

// check that a name was entered
if (empty($name))
    $error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
    $error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address))
    $error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
    $error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
    $error = 'You must enter a message.';

// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
    header('Location: contact.php?e='.urlencode($error)); exit;
}

$headers = "From: $email_address\r\n";
$headers .= "Reply-To: $email_address\r\n";

// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Phone Number: $phone\n";
$email_content .= "Message:\n\n$message";

// send the email
//ENTER YOUR INFORMATION BELOW FOR THE FORM TO WORK!
mail ('EMAIL', 'Young & Company - Contact Form Submission', $email_content, $headers);

// send the user back to the form
header('Location: index.html'/*.urlencode('Thank you for your message.'*/)); exit;

?>

【问题讨论】:

    标签: php html wordpress error-handling contact-form


    【解决方案1】:

    您需要查看PHP 错误日志。试试这个来显示错误。

    ini_set('display_errors', '1');
    

    如果您无法辨别哪些日志是相关的,请在此处发布您的日志。最有可能归咎于 SMTP 设置。 mail 函数打开一个套接字连接。使用 SMTP 设置。

    SMTP 设置可以通过 ini 或通过以下方式管理:

    ini_set('SMTP', 'smtphost');
    ini_set('smtp_port', 25);
    

    以上设置只是,例如,您需要有自己的 SMTP 设置。例如,如果您有一个 Gmail 帐户,您可以使用它来发送邮件。这取决于您可能要使用的 SMTP 服务器的具体情况。

    这里是Gmail SMTP settings。另外,请查看documentation

    【讨论】:

      【解决方案2】:

      contact-form-submission.php 文件的最后一行有错误。 您忘记从下面的行中删除多余的括号。

      // send the user back to the form
       header('Location: index.html'/*.urlencode('Thank you for your message.'*/)); exit;
      

      新的

      // send the user back to the form
      header('Location: index.html'/*.urlencode('Thank you for your message.'*/); exit;
      

      或者在注释标签中包含那个括号。

      // send the user back to the form
      header('Location: index.html'/*.urlencode('Thank you for your message.')*/); exit;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多