jQuery 3 + Ajax + PHP发送邮件表单不发送邮件

时间:2018-05-01 22:50:30

标签: php jquery ajax jquery-3

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

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

也许你可以看到我错过的东西?

以下代码主要遵循this example on Stack Overflow,但我也尝试过关注this youtubethis 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.";
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

感谢@Darren指出我的错误回复。

问题似乎只是我从this tutorial的第3步复制的from标头语法。

我改变了:

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

为:

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

我没有从列为重复的the question/answer中得到这个,这个答案很有用。

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