我的网页上的联系表单最终会发送电子邮件,但它并没有以正确的格式发送。以下是我收到的电子邮件。出于隐私原因,我模糊了电子邮件和组织。
无论如何,正如您从图片中看到的那样,该人在联系表单中写的姓名,电子邮件和邮件内容都在主题行而不是实际的电子邮箱中 - 我写下来了jibberjabber当我填写表格进行测试时。有没有办法可以格式化我的代码,以便他们记下的消息和名称在消息框而不是主题框中,并且电子邮件是从将电子邮件放在表单中的人发送的。非常感谢任何帮助,并提前感谢!
到目前为止,这是我的代码:
HTML
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php" accept-charset="UTF-8">
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<div class="col-md-6">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required="required" style="color: #ffffff"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email Address" required="required" style="color: #ffffff"/>
</div>
</div>
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required="required" style="color: #ffffff"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn-submit">Send Now</button>
</div>
</form>
CSS--我会把它放在这里以防万一。
#contact {
background-image: url(../images/contact-bg.jpg);
padding-bottom: 0;
height: auto;
background-attachment: fixed;
}
#contact .heading {
padding-top: 95px;
}
#main-contact-form {
text-align: center;
}
.contact-info {
text-align: left;
}
form {
align: center;
margin: 0 auto;
}
.form-control {
background-color: transparent;
border-color: rgba(255, 255, 255, .8);
height: 50px;
border-radius: 1;
box-shadow: none;
}
textarea.form-control {
min-height: 180px;
resize: none;
}
.form-group {
margin-bottom: 30px;
}
ul.address {
margin-top: 30px;
list-style: none;
padding: 0;
margin: 0;
}
.contact-info ul li {
margin-bottom: 8px;
}
.contact-info ul li a {
color: #fff;
}
.btn-submit {
display: block;
padding: 12px;
width: 100%;
color: #fff;
border: 0;
margin-top: 40px;
}
Java脚本
(function ($) {
var messages = {
'progress': '<p><i class="fa fa-spinner fa-spin"></i> Sending email...</p>',
'success': '<p class="text-success">Thank you for contacting us. We will contact you as early as possible.</p>',
'error': '<p class="text-danger">Aw snap! Something didn\'t go right!</p>'
};
var $form = $('form#main-contact-form');
var formStatus = $('<div class="form_status"/>');
$form.submit(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: $form.serialize(),
dataType: 'json',
beforeSend: function () {
formStatus.html(messages.progress).fadeIn().prependTo($form);
},
success: function (data) {
if (data.success === true) {
formStatus.html(messages.success);
} else {
formStatus.html(messages.error);
}
formStatus.fadeIn().delay(3000).fadeOut();
},
error: function () {
formStatus.html(messages.error).fadeIn().delay(3000).fadeOut();
}
});
})
})(jQuery);
PHP
<?php
$to = 'changedfor@privacyreasons.xyz';
$name = !empty($_POST['name']) ? filter_var(trim($_POST['name']), FILTER_SANITIZE_STRING) : '';
$from = !empty($_POST['email']) ? filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL) : $to;
$message = !empty($_POST['message']) ? filter_var(trim($_POST['message']), FILTER_SANITIZE_STRING) : '';
$body = "Name: {$name}\r\nEmail: {$from}\r\nMessage: {$message}";
$body = wordwrap($body, 70, "\r\n");
$headers = [
'MIME-Version: 1.0',
'Content-type: text/plain; charset=iso-8859-1',
"From: $name <$from>",
"Reply-To: <$from>",
'X-Mailer: PHP/' .phpversion()
];
$subject = 'some subject';
$success = mail($to, $body, implode("\r\n", $headers));
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
die(json_encode(['success' => $success]));
}
echo $success ? 'Sent Successfully.' : 'An error occurred';
ini_set('display_errors', 1); error_reporting(E_ALL);
?>
答案 0 :(得分:2)
您缺少主题参数,请阅读the docs。
此外,'\r\n'
与"\r\n"
$subject = 'some subject';
$success = mail($to, $subject, $body, implode("\r\n", $headers));