所以我有一个使用bootstrap的基本联系表单。我正在尝试通过电子邮件发送表单的内容,电子邮件不会发送。表单正在验证并显示成功消息,但电子邮件未发送。任何帮助,将不胜感激。我知道有关于php邮件表单的类似帖子,但我认为我的问题是在ajax帖子中。在PHP脚本中,一切似乎都没问题。
标记:
<h4>Contact Form</h4>
<div id="success"> </div> <!-- For success/fail messages -->
<form id="sentMessage" class="well" id="contactForm" novalidate="">
<div class="control-group">
<div class="controls">
<input type="text" class="form-control" placeholder="Full Name" id="name" required data-validation-required-message="Please enter your name" />
<p class="help-block"></p>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="email" class="form-control" placeholder="Email" id="email" required data-validation-required-message="Please enter your email" />
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="text" class="form-control" placeholder="Phone" id="phone" required data-validation-required-message="Please enter your phone" />
</div>
</div>
<div class="control-group">
<div class="controls">
<textarea rows="10" cols="100" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter your message" minlength="5" data-validation-minlength-message="Min 5 characters" maxlength="999" style="resize:none"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary pull-right" id="submit">Send</button><br />
</form>
我使用ajax从输入中获取数据并发布到PHP文件
$(function() {
$("input,textarea").jqBootstrapValidation(
{
preventSubmit: true,
submitError: function($form, event, errors) {
},
submitSuccess: function($form, event) {
event.preventDefault();
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();
var firstName = name;
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "bin/contact_me.php",
type: "POST",
data: {name: name, phone: phone, email: email, message: message},
cache: false,
success: function() {
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append( "</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
$('#contactForm').trigger("reset");
},
error: function() {
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append( "</button>");
$('#success > .alert-danger').append("<strong>Sorry "+firstName+" it seems that my mail server is not responding...</strong> Sorry for the inconvenience!");
$('#success > .alert-danger').append('</div>');
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
$('#name').focus(function() {
$('#success').html('');
});
然后我使用PHP发送电子邮件。
<?php
// check if fields passed are empty
if(empty($_POST['name']) ||
empty($_POST['phone']) ||
empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$message = $_POST['message'];
// create email body and send it
$to = 'myemail@somedomain.com'; // put your email
$email_subject = "Contact form submitted by: $name";
$email_body = "You have received a new message. \n\n".
" Here are the details:\n \nName: $name \n ".
"Email: $email_address\n Phone: \n $phone \n Message: \n $message";
$headers = "From: myemail@somedomain.com\n";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
提前谢谢你。