我正在尝试使用php mail()
函数向用户帐户发送电子邮件。邮件已成功发送,但问题是它向我发送了空白电子邮件,其中没有值!发送电子邮件的联系页面的代码如下: -
<form class="contact-form" method="POST" action="sendemail.php">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your First Name" name="name">
<label>Last Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your Last Name" name="lname">
<label>Email Address</label>
<input type="text" class="input-block-level" required="required" placeholder="Your email address" name="email">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required="required" class="input-block-level" rows="8" name="message"></textarea>
</div>
</div>
<input type="submit" class="btn btn-primary btn-large pull-right" value="Send Message" />
<p> </p>
</form>
和sendemail.php页面代码如下:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = "An enquiry sir";
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'email@email.com';
echo $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;
?>
为什么我的电子邮件ID中的输出变为空白,例如:
名称:
电子邮件:
主题:
消息:
P.N:我在这里使用的是新星模板主题。
表单是使用以下JavaScript通过AJAX提交的:
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 :(得分:0)
表单提交代码未提交表单数据。以下是您提供的代码:
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;
});
这应该是:
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;
});
答案 1 :(得分:-3)
从定义$ body的行中删除echo
从这......
echo $body = 'Name: ' . $name . "\n\n" . 'Email
对此...
$body = 'Name: ' . $name . "\n\n" . 'Email