我的联系表格在测试/PHPMailer后给了我一个内部服务器错误(500)

My contact form gives me an internal server error (500) after testing / PHPMailer

本文关键字:一个 内部 错误 服务器 表格 联系 测试 PHPMailer 我的      更新时间:2024-03-25

我以前做过自己的mail.php,但我一直在学习PHPMailer,并希望在我的表单上为我正在开发的新网站实现它。每次我通过输入信息并点击发送来测试它时,它都会给我一个内部服务器错误,并没有真正解释原因,但它可能与我的头有关。

我的网站/页面供参考

我的代码:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="php/mail.php" method="POST">
  <div class="form-group">
    <input type="text" class="form-control" id="name" placeholder="Full Name" required>
  </div>
  <div class="form-group">
    <input type="email" class="form-control" id="email" placeholder="Email Address" required>
    <small class="text-muted">I will never share your email with anyone. Pinky swear.</small>
  </div>
  <div class="form-group">
    <input type="phone" class="form-control" id="phone" placeholder="Phone Number" required>
  </div>
  <div class="form-group">
    <input type="text" class="form-control" id="company" placeholder="Company (optional)">
  </div>
  <div class="form-group">
    <div class="radio">
      <label>
        <input type="radio" name="radio" id="optionsRadios1" value="Project" checked>Project&nbsp;&nbsp;
      </label>
    </div>
    <div class="radio">
      <label>
        <input type="radio" name="radio" id="optionsRadios2" value="Collab">Collaboration&nbsp;&nbsp;
      </label>
    </div>
    <div class="radio">
      <label>
        <input type="radio" name="radio" id="optionsRadios3" value="Other">Other&nbsp;&nbsp;
      </label>
    </div>
  </div>
  <div class="form-group">
    <textarea class="form-control" id="message" rows="4" placeholder="What do you need?" required></textarea>
  </div>
  <button class="btn btn-primary" style="float: right;" type="submit">Send Away!</button>
</form>

这是我的php

<?php
//gather all form fields
$name = mysqli_real_escape_string($db, $_POST['name']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$phone = mysqli_real_escape_string($db, $_POST['phone']);
$company = mysqli_real_escape_string($db, $_POST['company']);
$radio = mysqli_real_escape_string($db, $_POST['radio']);	
$message = mysqli_real_escape_string($db, $_POST['message']);
if($name == "" || $email == "" || $phone == "" || $company == "" || $radio == "" || $message == "" ){
		//this could load a modal or some type of error pop up
		echo '<div class="alert alert-danger">The form submission is missing values.</div>';
        exit();
	} 
	else {
		
	//include autoload
	require 'php/PHPMailerAutoload.php';
	$mail = new PHPMailer;
	$mail->isMail();                                      // Set mailer to use SMTP
	
	$mail->setFrom('from@example.com', 'Mailer');
	$mail->addAddress('renegade13design@gmail.com', 'Joe User');     // Add a recipient
	
	$mail->isHTML(true);                                  // Set email format to HTML
	
	$mail->Subject = 'Here is the subject';
	$mail->Body    = '
		<!doctype html>
		<html>
		<head>
		<link rel="stylesheet" href="" type="text/css" media="screen, projection" />
		<style>
		h1 { font-size: 14px; }
		body {font-family: helvetica neue, arial;}
		</style>
		</head>
		<body>
		
		<table style="width: 600px; padding: 12px;" align="center">
			<tr>
				<td><img src="img/form.jpg"></td>
			</tr>
			<tr>
				<td><strong>Full Name:</strong> '.$name.'</td>
			</tr>
			<tr>
				<td><strong>Email Address:</strong> '.$email.'</td>
			</tr>
			<tr>
				<td><strong>Phone Number:</strong> '.$phone.'</td>
			</tr>
			<tr>
				<td><strong>Company:</strong> '.$company.'</td>
			</tr>
			<tr>
				<td><strong>What do you want?:</strong> '.$radio.'</td>
			</tr>
			<tr>
				<td><strong>What do you need?:</strong> '.$message.'</td>
			</tr>
		</table>
		
		</body>
		</html>			
	
	';
	
	if(!$mail->send()) {
		echo 'Message could not be sent.';
		echo 'Mailer Error: ' . $mail->ErrorInfo;
	} else {
		header( 'http://www.scratchmediaohio.com/twentysixteen' );
	}	
		
}
?>

您的某些输入没有"name"属性,因此它们不会是$_POST数组的一部分,因此不会出现在您的邮件脚本中。

<input type="text" class="form-control" id="name" placeholder="Full Name" required>

应该是

<input type="text" class="form-control" name="name" id="name" placeholder="Full Name" required>

对于您的输入:姓名、电子邮件、电话和公司-唯一有它们的是收音机选择。我还建议对您的输入进行比目前更多的验证。