Why is php's mail() function successfully sending mail but with blank fields?

我与影子孤独终老i 提交于 2019-12-10 16:18:03

问题


Email is arriving at the destination address but with blank fields. What is the cause?

My use of mail() is as follows:

<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);

$name = @trim(stripslashes($_POST['name'])); 
$email = @trim(stripslashes($_POST['email'])); 
$subject = @trim(stripslashes($_POST['subject'])); 
$message = @trim(stripslashes($_POST['message'])); 

$email_from = $email;
$email_to = 'info@siteaddress.com';

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

echo json_encode($status);
die; 
?>

And the form HTML is:

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required placeholder="Name" name="name" id="name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required placeholder="Email address" name="email" id="email">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="message" id="message" required class="form-control" rows="8" placeholder="Message" name="message" id="message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger btn-lg">Send Message</button>
</div>
</div>
</div>
</form>

回答1:


$subject = @trim(stripslashes($_POST['subject'])); but your form don't have subject, you should add it.
Don't suppress errors by @, because you never will know what exactly happens with your code.




回答2:


Finally Got the Answer.... Problem is that my form was not posting anything because the following script was missing

<script>
var form = $('.contact-form');
form.submit(function () {
    $this = $(this);
    $.post($(this).attr('action'),$(this).serialize(), function(data) {
        $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
    },'json');
    return false;
});
</script>

i have added $(this).serialize() to the script and now the mail is working properly...

Thanks all of you....




回答3:


Hi I´m have the same problem on a 000webhost app. I solve this with two moodifications 1rst: Add var name = $("input#name").val(); for a form values and in to the ajax function name: name, for a form values

var form = $('#main-contact-form');
form.submit(function(event){
    event.preventDefault();     
        var name = $("input#name").val();
        var email = $("input#email").val();
        var subject = $("input#subject").val();
        var message = $("textarea#message").val();

    var form_status = $('<div class="form_status"></div>');
    $.ajax({
         url: $(this).attr('action'),
            type: "POST",
            data: {
                name: name,
                email: email,
                subject: subject,
                message: message,
            },
            cache: false,

        beforeSend: function(){
            form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email enviandose</p>').fadeIn() );

        }
    }).done(function(data){
        form_status.html('<p class="text-success"> Gracias por la consulta, a la brevedad estaremos respondiendo</p>').delay(3000).fadeOut();
             //clear all fields
            $('#main-contact-form').trigger("reset");
    });
});  

2nd: The tag id"name" id"email" id"XX" in mi Form in my case




回答4:


    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

     //to Replace This

    $header  .= 'MIME-Version: 1.0' . "\r\n";
    $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>',,$header);
 if( $success == true )  
               { 
//success Message
}
else{
//Error Message
}


来源:https://stackoverflow.com/questions/29815710/why-is-phps-mail-function-successfully-sending-mail-but-with-blank-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!