发送php电子邮件时,它是空白的

时间:2016-12-22 17:14:22

标签: javascript php

当我使用联系表单发送电子邮件时,其自身的电子邮件是空白的(没有主题,没有任何消息或任何内容!)电子邮件完美地发送到我的Gmail,但如上所述,没有主题等。

JS

$('#contactform').submit(function(){
    var action = $(this).attr('action');
    $("#message").slideUp(250,function() {
        $('#message').hide();
        $('#submit')
            .after('<img src="img/assets/cbp-loading.gif" class="loader" />')
            .attr('disabled','disabled');
        $.post(action, {
            name: $('#name').val(),
            email: $('#email').val(),
            subject: $('#subject').val(),
            comments: $('#comments').val(),
        },
            function(data){
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown(250);
                $('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
                $('#submit').removeAttr('disabled');
                if(data.match('success') != null) $('#contactform').slideUp(850, 'easeInOutExpo');
            }
        );
    });

PHP

<?php
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['_replyto']) ? $_POST['_replyto'] : '';
$subject = isset($_POST['_subject']) ? $_POST['_subject'] : '';
$comments = isset($_POST['comments']) ? $_POST['comments'] : '';

$to = 'WhereTheInfoWillBeSent@gmail.com';
$subject = $subject;
$comments = "From: " . $name . ", " . $email . "\r\n \r\n" . "comments: " . $comments;
if (mail($to, $email, $subject, $comments)) {
    echo "Thanks for contacting us, we will get back to you soon!";
} else {
    echo "Sorry, there was an error sending the email. Please ensure you have filled out the form correctly and try again!";
}

HTML

form method="post" action="contact-form.php" name="contactform" id="contactform">
                            <fieldset>
                                    <input name="name" type="text" id="name" placeholder="Name"/> 
                                    <input name="email" type="text" id="email" placeholder="Email"/>  
                                    <input name="subject" type="text" id="subject" placeholder="Subject"/> 
                            </fieldset>
                            <fieldset> 
                                    <textarea name="comments" cols="40" rows="3" id="comments" placeholder="Message"></textarea>
                            </fieldset>
                            <input type="submit" class="submit" id="submit" value="Send Message" />
                        </form>
                    </div>  

2 个答案:

答案 0 :(得分:1)

不是很明显,您发布的数据与您的预期不同。

{
     name: $('#name').val(),
     email: $('#email').val(),
     subject: $('#subject').val(),
     comments: $('#comments').val(),
}

这意味着只有$_POST中的这些字段可用。 现在要么在JS中更改密钥名称,要么在PHP中更改密钥名称。

$email = isset($_POST['email']) ? $_POST['email'] : '';
$subject = isset($_POST['subject']) ? $_POST['subject'] : '';

答案 1 :(得分:0)

除了@anwerjunaid所说的,你需要改变PHP的一行顺序:

此:

    if (mail($to, $email, $subject, $comments)) {
    echo "Thanks for contacting us, we will get back to you soon!";
} else {
    echo "Sorry, there was an error sending the email. Please ensure you have filled out the form correctly and try again!";
}

应该是:

if (mail($to, $subject, $comments)) {
    echo "Thanks for contacting us, we will get back to you soon!";
} else {
    echo "Sorry, there was an error sending the email. Please ensure you have filled out the form correctly and try again!";
}

订单需要是收件人,电子邮件主题和邮件内容。

如果您想要包含来自或回复,请添加以下内容:

$headers = 'From: WhereTheInfoWillBeSent@gmail.com' . "\r\n" .
'Reply-To: WhereTheInfoWillBeSent@gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $comments, $headers);