提交电子邮件时收到500错误但电子邮件仍然存在

时间:2015-02-23 05:26:30

标签: javascript php ajax

我迷失了为什么我得到这个内部错误(500)。点击提交后,我的电子邮件信息仍然存在。我唯一的问题是没有调用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.";
    }

?>

0 个答案:

没有答案