简体   繁体   English

使用联系表 Submission.php 收到 500 服务器错误

[英]Receiving 500 Server Error with Contact Form Submission.php

I've looked through a bunch of similar issues but haven't been able to find an answer that works.我已经浏览了一堆类似的问题,但一直无法找到有效的答案。 I recently switched my website over to a different host and am trying to set up the contact form.我最近将我的网站切换到其他主机,并尝试设置联系表格。 I am getting a 500 error when I click submit.单击提交时出现 500 错误。 I've made sure the SMTP is set up correctly.我已经确保 SMTP 设置正确。 I just don't know what else to try at this point.我只是不知道此时还能尝试什么。 Here's my code:这是我的代码:

HTML: 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:

<?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;

?>

You need to look into PHP error log.您需要查看PHP错误日志。 Try this to show errors.试试这个来显示错误。

ini_set('display_errors', '1');

Post your logs here if you can't discern which logs are relevant.如果您无法辨别哪些日志是相关的,请在此处发布您的日志。 Most likely SMTP settings are be blamed.最有可能的原因是 SMTP 设置。 mail function opens a socket connection.邮件函数打开一个套接字连接。 using SMTP settings.使用 SMTP 设置。

SMTP settings can be managed through ini or by:可以通过 ini 或通过以下方式管理 SMTP 设置:

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

The above settings are just, for example, you need to have your own SMTP settings.以上设置只是,例如,您需要有自己的 SMTP 设置。 For example, if you have a Gmail account, you may use it to send mail.例如,如果您有 Gmail 帐户,则可以使用它来发送邮件。 It depends on your specific situation on which SMTP server you may want to use.这取决于您可能要使用的 SMTP 服务器的具体情况。

Here are the Gmail SMTP settings .以下是Gmail SMTP 设置 Also, look at the documentation .另外,请查看文档

There is an error on the last line of contact-form-submission.php file. contact-form-submission.php文件的最后一行有错误。 You forget to remove the extra bracket from the below line.您忘记从下面的行中删除额外的括号。

Old老的

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

New新的

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

Or include that bracket in comment tag.或者在注释标签中包含该括号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM