电子邮件表格PHP无效

时间:2015-12-09 13:15:13

标签: javascript php forms email

我正在修建一个我正在建设的网站。我正在使用一些代码,它不适用于电子邮件表单。 javascript正在运行,但是当我尝试调用PHP时它没有运行。

Javascript如下:

$(function() {

    $("#contactForm input,#contactForm textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var desc = $("textarea#desc").val();
            var peoplefeed = $("input#peoplefeed").val();
            var datepickup = $("input#datepickup").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                console.log("php go!")
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    desc: desc,
                    peoplefeed: peoplefeed,
                    datepickup: datepickup
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

运行正常,console.log正常运行

这是我的PHP:

<?php
// check if fields passed are empty
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['desc'])        ||
   empty($_POST['peoplefeed'])  ||
   empty($_POST['datepickup'])  ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";

    return false;
   }

    $name = $_POST['name'];
    $email_address = $_POST['email'];
    $phone = $_POST['phone'];
    $desc = $_POST['desc'];
    $peoplefeed = $_POST['peoplefeed'];
    $datepickup = $_POST['datepickup'];

    // create email body and send it    
    $to = "freakcakes@patissez.com.au"; // *REPLACE WITH THE EMAIL ADDRESS YOU WANT THE FORM TO SEND MAIL TO*
    $email_subject = "Cake Order for: $name";
    $email_body = "You have received a new Cake Order\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$desc\n\nPhone: \n$desc\n\nPeople to Feed:\n$peoplefeed\n\nPickup Date:\n$datepickup";
    $headers = "From: noreply@patissez.com.au\n"; // *REPLACE WITH THE EMAIL ADDRESS YOU WANT THE MESSAGE TO BE FROM*
    $headers = "Reply-To: $email_address";  
    mail($to,$email_subject,$email_body,$headers);
    return true;            
    ?>

但是,当我填写所有内容时,电子邮件都没有发送,域名电子邮件中没有反垃圾邮件,我查看了所有内容,无法找到它被捕获的地方

1 个答案:

答案 0 :(得分:0)

这样做

if(empty($_POST['name'])||empty($_POST['email'])|| empty($_POST['phone']) || empty($_POST['desc'])        ||
    empty($_POST['peoplefeed']) || empty($_POST['datepickup']) || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
    echo "No arguments Provided!";
    # return false;
}
else{

    $name = $_POST['name'];
    $email_address = $_POST['email'];
    $phone = $_POST['phone'];
    $desc = $_POST['desc'];
    $peoplefeed = $_POST['peoplefeed'];
    $datepickup = $_POST['datepickup'];

    $to = "freakcakes@patissez.com.au";
    $email_subject = "Cake Order for: $name";
    $email_body = "You have received a new Cake Order\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$desc\n\nPhone: \n$desc\n\nPeople to Feed:\n$peoplefeed\n\nPickup Date:\n$datepickup";
    $headers = "From: noreply@patissez.com.au\r\n";
    $headers .= "Reply-To: $email_address"; # changed

    if( mail($to,$email_subject,$email_body,$headers)) # changed
    {
        echo "Success";
    }
    else{
        echo "Failed";
    }

}