PHP Mail不使用AJAX发送邮件

时间:2016-07-18 08:25:39

标签: php ajax email

美好的一天,所以我偶然发现了这个非常简单的问题。需要注意的是,我一直在研究这个问题,但我不明白为什么这不起作用。所以我正在做的是尝试将评论表单成功发送到我的电子邮件。所以这是我的代码:

HTML

<input type="text" id="his-name" name="his-name" placeholder="His name*">
<input type="text" id="her-name" name="her-name" placeholder="Her name*">
<input type="text" id="location" name="location" placeholder="Location of honeymoon*">
<textarea id="fav-moment" name="fav-moment" placeholder="Favorite moment during the trip"></textarea>  
<textarea id="review" name="review" placeholder="Please write a short review of your experience*"></textarea>
<p>Upload few pictures from your honeymoon <span style="display: block; font-size:.7em">Please upload only a maximum of 4 photos</span></p><br/>

<form action="<?php echo get_template_directory_uri(); ?>/upload.php?sessionid=<?php echo $_SESSION['sessionid'] ?>"
             class="dropzone" id="my-awesome-dropzone"></form>

JS

$(document).ready(function () {
showMenu();

$('#submit-feedback').click(function (e) {
    e.preventDefault();
    ajax_send_feedback();
});

function ajax_send_feedback() {
$('#contact-msg-f').html('<i style="font-size:2em;" class="fa fa-spinner fa-spin"></i>');
var errors = new Array();
errors = validate_feedback_form();
if (errors.length != 0) {
    display_feedback_errors(errors);
    feedbackDelivered(0);
    return;
}
else {
    // Send data for server validation
    $.get("http://atravelduet.com/process-review-form.php", {
        hisname: $('#his-name').val(),
        hername: $('#her-name').val(),
        location: $('#location').val(),
        favmoment: $('#fav-moment').val(),
        review: $('#review').val(),
        url: $('#url').val()
    }).done(function (response) {
            if (!response) {
                feedbackDelivered(1);
                clearFields(); **//THIS IS NOT WORKING TOO :(**
            }
            else {
                var errros = new Array();
                errors = $.parseJSON(response);
                if (errors.length != 0) {
                    display_feedback_errors(errors);
                    feedbackDelivered(0);
                }
            }
        });
    }
}

function feedbackDelivered(res) {
if (res == 1) {
    $('#contact-msg-f').html('<span style="color:#00ff00;">Message Sent!</span>');
}
else {
    $('#contact-msg-f').html('<span style="color:#ff0000;">Please correct the fields marked red!</span>');
}
}

PHP

<?php
$email0 = 'codeaxiscom@gmail.com';
$email1 = 'jjmaranga05@gmail.com';

$his_name = $_POST['hisname'];
$her_name = $_POST['hername'];
$location = $_POST['location'];
$fav_moment = $_POST['favmoment'];
$review = $_POST['review'];

$body = <<< EOD
<br><hr><br>
His Name: $his_name<br>
Her Name: $her_name<br>
Location: $location<br>
Favorite Moment: $fav_moment<br>
Review: $review<br>
EOD;

$headers = "From: calcutt5@box996.bluehost.com\r\n";
$headers .= "Content-type: text/html\r\n";
$emailSubject = "$his_name and $her_name Honeymoon Review";
$emailTo = $email1;

if(mail($emailTo, $emailSubject, $body, $headers)) 
    echo "Successful";
else
    echo "Unsuccessful";
?>

1 个答案:

答案 0 :(得分:0)

首先你有一个错误,因为mail()函数返回FALSE。所以没有发送电子邮件。

尝试将&#34; From&#34;像这样的内容类型之后的标题

$headers = "Content-type: text/html\r\n";
$headers .= "From: calcutt5@box996.bluehost.com\r\n";

并检查它现在是否正常工作。如果没有完全尝试comment the line with From header并检查它是否会返回成功,并且您收到了电子邮件。

问题可能是你使用的不好&#34;来自&#34;头。在大多数情况下,电子邮件应该像发送邮件的域一样。

此外,我建议你使用一个PHPMailer,这个配置并不难,而且效果要好得多,而且你有一个错误日志,所以你可以轻松调试它。