为什么php的mail()函数成功发送邮件但是有空白字段?

时间:2015-04-23 06:46:39

标签: php email error-handling sendmail

电子邮件到达目的地地址但有空白字段。原因是什么?

我对mail()的使用如下:

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

表格HTML是:

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

4 个答案:

答案 0 :(得分:3)

$subject = @trim(stripslashes($_POST['subject']));但您的表单没有主题,您应该添加它。
不要用@来抑制错误,因为你永远不知道你的代码到底发生了什么。

答案 1 :(得分:0)

终于得到了答案....问题是我的表单没有发布任何内容,因为以下脚本丢失了

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

我已将$(this).serialize()添加到脚本中,现在邮件正常运行...

谢谢你们所有人......

答案 2 :(得分:0)

您好我在000webhost应用上遇到了同样的问题。我用两种情绪来解决这个问题 1RST: 添加var name = $(“input#name”)。val();对于表单值和ajax函数名称:name,表单值

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

第二名:我的案例中的mi表格中的标签ID“name”id“email”id“XX”

答案 3 :(得分:-1)

    $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
}