首页 > 解决方案 > 代码审查 - 在电子邮件脚本上不断出现 500 错误

问题描述

我使用 PHP/AJAX 从表单中通过电子邮件发送一些信息。下面的基本表单,AJAX 和 PHP 脚本,不断收到 500 错误,我不知道为什么!我经常使用相同的设置或非常相似的设置,我已经在服务器上测试了“邮件”功能,并且工作正常。

任何帮助都是极好的!

谢谢

// quickquote form send
$(function() {

// Get the form.
var form = $('#quickquote');

// Get the messages div.
var formMessages = $('#form-messages');

// Set up an event listener for the contact form.
$(form).submit(function(e) {
    // Stop the browser from submitting the form.
    e.preventDefault();

    // Serialize the form data.
    var formData = $(form).serialize();

    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData
    })
    .done(function(response) {
        // Make sure that the formMessages div has the 'success' class.
        $(formMessages).removeClass('alert alert-danger');
        $(formMessages).addClass('alert alert-success');

        // Set the message text.
        $(formMessages).text(response);

        // Clear the form.
  $('#quickquote')[0].reset();
     })
    .fail(function(data) {
        // Make sure that the formMessages div has the 'error' class.
        $(formMessages).removeClass('alert alert-success');
        $(formMessages).addClass('alert alert-danger');

        // Set the message text.
        if (data.responseText !== '') {
            $(formMessages).text(data.responseText);
        } else {
            $(formMessages).text('Oops! An error occured and your message could not be sent.');
        }
    });

});

});

<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection

    // Get the form fields and remove whitespace.
    $name = $_POST['name'];
    $email = $_POST['email'];
    $tel = $_POST["phone"];
    $address = $_POST["address"];
    $length = $_POST["length"];
    $width = $_POST["width"];

    // Set the recipient email address.
    // Update this to your desired email address.
    $recipient = "email@email.com";

    // Set the email subject.
    $subject = "Contact Form Request from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n";
      $email_content .= "Telephone: $tel\n";
    $email_content .= "Address: $address\n";
    $email_content .= "Length: $length\n";
      $email_content .= "Width: $width\n\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

标签: phpemail

解决方案


推荐阅读