jQuery3+Ajax+PHP发送邮件形式不发送邮件

jQuery 3 + Ajax + PHP send mail form not sending mail

我一直在尝试重写 jQuery + PHP 发送邮件表单,但尽管尝试了 3 种方法,它仍然抛出 500 内部服务器错误。我正在重写,因为我正在将网站迁移到 jQuery 3 并致力于提高性能和标准符合我继承的代码库。

我知道 500 是服务器错误,站点托管在 MS IIS 上,但遗留代码在同一台服务器上运行,因此一定有一些特定于我的实现。

也许你能看到我遗漏的东西?

下面的代码大多遵循 this example on Stack Overflow but I've also tried following this youtube and this tutorial 但没有成功。

希望在用尽我的其他选择后在这里得到答案。提前致谢!

HTML(简体)

<form action="send_mail_facility2.php" id="facility-contact-form" method="post" autocomplete="on">
  <!-- MAIN FIELDS -->
  <input id="sendTo" name="sendTo" type="text" value="Facility One" style="display: none;">
  <input id="name" name="name" type="text" placeholder="Name" required>
  <input id="email" name="email" type="email" placeholder="Email" required>
  <input id="phone" name="phone" type="text" placeholder="Phone number">
  <textarea id="message" name="message" type="text" placeholder="Message"></textarea>

  <!-- ADDITIONAL FIELDS - separate js shows section when checkbox selected -->
  <input id="infopack-checkbox" type="checkbox" name="checkbox" value="checkbox"><span class="information-pack">&nbsp;I would like to receive an information pack.</span>
  <div id="address-details">
    <input id="address" name="address" type="text" placeholder="Address">
    <input id="city" name="city" type="text" placeholder="City">
    <input id="postcode" name="postcode" type="text" placeholder="Postcode">
  </div>

  <!-- reCAPTCHA html & js -->

  <input id="facility-submit" name="submit" type="submit" value="Send request" disabled style="opacity: 0.3;"> <!-- disabled & inline style for captcha -->
  <p class="status" style="display: none" role="alert">Thanks, someone will be in contact with you regarding your enquiry soon.</p>
</form>

JS

$(function(){
  var request;
  $("#facility-contact-form").on("submit", function(event){
    event.preventDefault();
    if (request) {
      request.abort();
    }
    var $form = $(this);
    var $inputs = $form.find("input, select, button, textarea");
    var serializeData = $form.serialize();
    $inputs.prop("disabled", true);
    request = $.ajax({
      url: "send_mail_facility2.php",
      type: "post",
      data: serializeData
    });
    request.done(function (response, textStatus, jqXHR){
      console.log("Hooray, it worked!");
    });
    request.fail(function (jqXHR, textStatus, errorThrown){
      console.error("The following error occurred: "+
                   textStatus, errorThrown);
    });
    request.always(function () {
      $inputs.prop("disabled", false);
    });
  });
});

PHP

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  // email headers
  $to       = $_POST["sendTo"];
  $subject  = "Custom subject line";
  $headers  = "From: NoReply &lt;noreply@noreply.com&gt;";

  // sender data
  $name     = $_POST["name"];
  $email    = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
  $phone    = trim($_POST["phone"]);
  $message  = trim($_POST["message"]);
  $address  = isset($_POST["address"]) ? trim($_POST["address"]) : null;
  $city     = isset($_POST["city"]) ? trim($_POST["city"]) : null;
  $postcode = isset($_POST["postcode"]) ? trim($_POST["postcode"]) : null;

  //define recipients
  $sendTo = $_POST["sendTo"];

  if($sendTo == 'Facility One') {
    $to = 'mytestemail@example.com';
  } elseif($sendTo == 'Facility Two') {
    $to = 'two@clientemail.com';
  } elseif($sendTo == 'Facility Three') {
    $to = 'three@clientemail.com';
  }

  if (isset($_POST['checkbox'])) {
    $infoPack = "Yes";
  } else {
    $infoPack = "No";
  }
  if (empty($_POST["address"])) {
    $address = "N/A";
  } else {
  }
  if (empty($_POST["city"])) {
    $city = "N/A";
  } else {
  }
  if (empty($_POST["postcode"])) {
    $postcode = "N/A";
  } else {
  }

  // email content
  $content = "Enquiry for facility: $sendTo\n\n";
  $content .= "Name: $name\n\n";
  $content .= "Email: $email\n\n";
  $content .= "Phone: $phone\n\n";
  $content .= "Message:\n$message\n\n";
  $content .= "Would user like to receive an information pack?: $infoPack\n\n";
  $content .= "Address: $address\n\n";
  $content .= "City: $city\n\n";
  $content .= "Postcode: $postcode\n\n\n";
  $content .= "Static footer message here\n\n";

  // Send the email
  $success = mail($to, $subject, $content, $headers);
  if ($success) {
    http_response_code(200);
    echo "Thanks, someone will be in contact with you regarding your enquiry soon.";
  } else {
    http_response_code(500);
    echo "Oops, something went wrong - we couldn't send your message.";
  }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "Oops, something went wrong - we couldn't send your message.";
}

有什么想法吗?

感谢@Darren 指出错误响应。

问题似乎是我从 this tutorial.

的第 3 步复制的 from header 语法

我改了:

$headers  = "From: NoReply &lt;noreply@noreply.com&gt;";

至:

$headers = "FROM: NoReply <noreply@noreply.com>";

我没有从 the question/answer 那里得到这个被列为重复的答案,尽管这个答案很有用。

对于其他 PHP 新手,使用 Firefox/Chrome 开发者工具网络选项卡,运行 通过提交您的表单,过滤您的 php 文件,然后查看响应选项卡以查看包含任何错误的完整响应。