问题描述
我已经浏览了许多类似的问题,但一直没有找到有效的答案。最近,我将网站切换到其他主机,并试图设置联系表。单击提交时出现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 @R_848_4045@ION 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
错误日志。尝试显示错误。
ini_set('display_errors','1');
如果您无法确定哪些日志是相关的,请在此处发布日志。最有可能的原因是SMTP设置被指责。邮件功能打开套接字连接。使用SMTP设置。
SMTP设置可以通过ini或通过以下方式进行管理:
ini_set('SMTP','smtphost');
ini_set('smtp_port',25);
例如,以上设置仅是您需要自己的SMTP设置。例如,如果您有一个Gmail帐户,则可以使用它来发送邮件。这取决于您要使用哪种SMTP服务器的具体情况。
这里是Gmail SMTP settings。另外,请查看documentation。
, 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;