简体   繁体   English

发送php电子邮件时为空

[英]when sending php email it is blank

When i send an email using the contact form, the email its self is blank (no subject and no message or anything!) The email sends perfectly to my gmail but as mentioned, no subject etc. 当我使用联系表发送电子邮件时,电子邮件的自身为空白(无主题,无消息或任何内容!)电子邮件完美地发送到了我的gmail,但如上所述,没有主题等。

JS 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

<?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 的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>  

Isn't it obvious, the data you posting is different from what you expecting. 这不是很明显,您发布的数据与您期望的不同。

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

It means that only these fields with be available in $_POST . 这意味着$_POST中只有这些字段可用。 Now either you change key name here in JS or change it in PHP like. 现在,您既可以在JS中更改键名,也可以在PHP中更改键名。

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

In addition to what @anwerjunaid said, you need to change the order of one line of your PHP: 除了@anwerjunaid所说的以外,您还需要更改PHP一行的顺序:

This: 这个:

    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!";
}

Should be: 应该:

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!";
}

The order needs to be recipient, email subject, and the message content. 订单必须是收件人,电子邮件主题和邮件内容。

If you want to include a from or reply to, then add something like this: 如果要包括“发件人”或“回复”,请添加以下内容:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM