簡體   English   中英

發送php電子郵件時為空

[英]when sending php email it is blank

當我使用聯系表發送電子郵件時,電子郵件的自身為空白(無主題,無消息或任何內容!)電子郵件完美地發送到了我的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>  

這不是很明顯,您發布的數據與您期望的不同。

{
     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'] : '';

除了@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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM