我正在制作一个通过PHP验证的表单。问题是发送时一切正常,但收到的电子邮件是空白的。这里有更多解释的代码:
表格:
<div class="status alert alert-success" style="display: none"></div>
<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" name="name" class="form-control" required="required" placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" class="form-control" pattern="[^ @]*@[^ @]*" name="email" required="required" placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input id="form-email" class="form-control" type="text" name="subject" size="64" maxlength="64" placeholder="Subject" required />
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<textarea name="body" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" name="send" class="btn btn-danger btn-lg">Send Message</button>
</div>
</div>
</div>
</form>
PHP文件(sendemail.php):
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$recipient = 'my_email@gmail.com';
$email = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$body = $_POST['body'];
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $body;
$success = mail($recipient, $subject, $body, 'From: <'.$email.'>');
echo json_encode($status);
die;
?>
电子邮件已在gmail帐户中重播:
Name:
电子邮件:
主题:
消息:
空白电子邮件。
任何建议/解决方案都将非常感谢!!
添加了ajax内容(忘记添加):
//Ajax contact
var form = $('.contact-form');
form.submit(function () {
$this = $(this);
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
答案 0 :(得分:1)
header('Content-type: application/json');`
$status = array();
$recipient = 'my_email@gmail.com';
$email = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$body = $_POST['body'];
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $body;
$success = mail($recipient, $subject, $body);
if($success)
{
$status['type']='success';
$status['message']='Send';
}
else
{
$status['type']='failed';
$status=$_POST;
}
echo json_encode($status);
die;
`
答案 1 :(得分:0)
尝试检查数据是否为空
$email = (isset($_POST['email']))?$_POST['email']:false;
$name = (isset($_POST['name']))?$_POST['name']:false;
$subject = (isset($_POST['subject']))?$_POST['subject']:false;
if($email || $name || $subject{
echo "One of them was not set";
}else if($email=="" || $name=="" || $suject==""){
echo "One of them was empty set";
} else{
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $body;
$success = mail($recipient, $subject, $body, 'From: <'.$email.'>');
}