Javascript 提交电子邮件时出现500错误,但电子邮件仍会通过

Javascript 提交电子邮件时出现500错误,但电子邮件仍会通过,javascript,php,ajax,Javascript,Php,Ajax,我不明白为什么我会得到这个内部错误。当我点击提交时,我的电子邮件信息仍然会通过。我唯一的问题是没有调用ajax成功方法,因此基本上我的回调函数永远不会被调用。任何帮助都将不胜感激。在这个问题上,我费了好几个小时的劲 我的scipt ajax调用: emailValidation: function(e){ e.preventDefault(); $('body, html').animate({scrollTop:0},"slow"

我不明白为什么我会得到这个内部错误。当我点击提交时,我的电子邮件信息仍然会通过。我唯一的问题是没有调用ajax成功方法,因此基本上我的回调函数永远不会被调用。任何帮助都将不胜感激。在这个问题上,我费了好几个小时的劲

我的scipt ajax调用:

        emailValidation: function(e){
            e.preventDefault();
            $('body, html').animate({scrollTop:0},"slow");
            var valid = '';
            var name = $("#f_name").val();
            var email = $("#f_email").val();
            var message = $("#f_message").val();
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

            if(name === '' || name.length <= 2){
                valid += '<p class="error">Name must be longer than 2 char.</p>';
            }
            if(message === '' || message.length <= 5){
                valid += '<p class="error">Message must be longer than 5 char.</p>';
            }
            if (!(email).match(emailReg)){
                valid += '<p class="error">Invalid Email</p>';
            }
            if (valid !== ''){
                $('#form-messages').html(''+valid+'').fadeIn();
            }
            else {
                var formData = $("#contact").serialize();//Value for sanitized form values to be paased to email.php. Value returns an array
                portfolio.submitEmail(formData);
            }
        },
        submitEmail: function(formData){
            console.log(formData);
            //$('#form-messages').html("Proccessing...<img src='img/processing_bar.png' alt='Proccessing' />").fadeIn('fast');
            $('body, html').animate({scrollTop:0},"fast");
            $.ajax({
                type: 'POST',
                url: 'mailer.php',
                data: formData,
                dataType: 'json',
                success: function(result){
                    console.log(result.statusText);
                    if(result.statusText === 'OK'){
                        console.log('jam');
                        // Make sure that the formMessages div has the 'success' class.
                        $('#form-messages').removeClass('error');
                        $('#form-messages').addClass('success');
                        // Set the message text.
                        $('#form-messages').html('<p class="success">Message has been sent succesfully! Thank you '+ $('#f_name').val() +', a response will be returned in less than one business day.</p>');
                        $("#contact").fadeOut('slow').remove();
                        $('body, html').animate({scrollTop:0},"fast");
                    }

                },
                error: function(error){
                    console.log(error.statusText);
                    if(error.statusText === 'OK'){
                        // Make sure that the formMessages div has the 'success' class.
                        $('#form-messages').removeClass('error');
                        $('#form-messages').addClass('success');
                        // Set the message text.
                        $('#form-messages').html('<p class="success">Message has been sent succesfully! Thank you '+ $('#f_name').val() +', a response will be returned in less than one business day.</p>');
                        $("#contact").fadeOut('slow').remove();
                        $('body, html').animate({scrollTop:0},"fast");
                    }
                },
            });
        },
这是我的mail.php文件:

<?php
    // Only process POST reqeusts.
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Get the form fields and remove whitespace.
        $em_name = strip_tags(trim($_POST["name"]));
        $em_name = str_replace(array("\r","\n"),array(" "," "),$em_name);
        $em_email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
        $em_message = $_POST["message"];
        $em_phone = $_POST['phone'];
        $em_website = $_POST['website'];
        $em_hear = $_POST['hear'];
        $em_startdate = $_POST['startdate'];
        $em_budget = $_POST['budget'];

        $to = 'theller5567@gmail.com';

        $subject = 'Website Change Request';

        $headers = "From: " . $em_email . "\r\n";
        $headers .= "Reply-To: ". $em_email . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

        // Build the email content.
        $message =  "<h3>Name: ". $em_name. "\n</h3>";
        $message .= "<h3>Message: ". $em_message. "\n</h3>";
        $message .= "<p>Budget: ". $em_budget. "\n</p>";
        $message .= "<p>Start Date: ". $em_startdate. "\n</p>";
        $message .= "<p>How did you hear about us?: ". $em_hear. "\n</p>";
        $message .= "<p>Email: ". $em_email. "\n</p>";
        $message .= "<p>Phone: ". $em_phone. "\n</p>";
        $message .= "<p>Website: ". $em_website. "\n</p>";

        if (mail($to, $subject, $message, $headers)) {
          http_response_code(200);
          echo "Thank You! Your message has been sent.";
        } else {
            http_response_code(500);
            echo "Oops! Something went wrong and we couldn't send your message."; 
        }
    } else {
       http_response_code(403);
        echo "There was a problem with your submission, please try again.";
    }

?>

我猜这个表达式:mail$to、$subject、$message、$headers不知何故返回false,这导致了到处都是问题。但不确定它为什么会返回false。让我们在那里放置一个var_转储,以了解邮件函数返回的是什么。