我正在尝试在jquery,ajax和php中创建一个联系我们表单。当我点击html jquery中的按钮和ajax向php发出请求然后我使用php mail()函数发送电子邮件。但是,当我点击"发送"它说一切正常,ajax函数成功函数运行警告用户消息已发送。现在,当我进入应该发送的收件箱时,我没有收到任何电子邮件。我在Windows 7,ecowebhost上运行它。
这是html表单;
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src = "js/callmail.js"></script>
</head>
<body ng-app = "">
<div class = "container">
<form name = "contact">
<label for = "subject" class = "control-label">Subject:</label>
<input name = "subject" id = "subject" ng-model = "subject" required/>
<span class = "warning" ng-show="contact.subject.$touched && contact.subject.$invalid">You Must Enter a subject!</span>
<span class = "success" ng-show="contact.subject.$touched && contact.subject.$valid">Valid!</span>
<br/>
<label for = "body" class = "control-label">Body:</label>
<input name = "body" id = "body" ng-model = "body" required/>
<span class = "warning" ng-show="contact.body.$touched && contact.body.$invalid">Your email must have a body!</span>
<span class = "success" ng-show="contact.body.$touched && contact.body.$valid">Valid!</span>
<br/>
<label for = "signature" class = "control-label">Return Email:</label>
<input name = "signature" id = "signature" ng-model = "signature" type = "email" required/>
<span class = "warning" ng-show="contact.signature.$touched && contact.signature.$invalid">You must enter a valid return email!</span>
<span class = "success" ng-show="contact.signature.$touched && contact.signature.$valid">Valid!</span>
<br/>
<button ng-disabled = "contact.$invalid" id = "sendmail" type = "submit" class = "btn btn-success">Submit</button>
</form>
这里是ajax和jquery:
$(function() {
$("#sendmail").click(function() {
var data = {
to: "tyler.psu.grim@gmail.com",
subject: $("#signature").val() + ": " + $("#subject").val(),
message: $("#body").val(),
};
$.ajax({
type: "POST",
url: "email.php",
data: data,
success: function() {
alert("Message sent");
}
});
console.log(data);
});
});
这是php代码:
<?php
if ($_POST["submit"]) {
$subject = $_POST["subject"];
$message = $_POST["body"];
$headers = "Reply to: " . $_POST["signature"];
mail("tyler.psu.grim@gmail.com", $subject, $message, $headers);
}
?>
这些语言都很新,所以我完全不知道从哪里开始。
答案 0 :(得分:-2)
$(function() {
$("#sendmail").click(function() {
var to = "tyler.psu.grim@gmail.com";
var subject = $("#signature").val() +": " + $("#subject").val(); //?? WHERE IS SUBJECT INPUT????
var message = $("#body").val();
var signature = $("#signature").val();
var data = {
to: to,
subject: subject,
message: message,
signature: signature
};
$.ajax({
type: "POST",
url: "email.php",
data: data,
success: function(msg) {
console.log(msg);
}
});
console.log(data);
return false;
});
});
/////
if(isset($_POST["subject"])) {
$subject = $_POST["subject"];
$message = $_POST["body"];
$headers = "Reply to: " . $_POST["signature"];
if(!mail("tyler.psu.grim@gmail.com", $subject, $message, $headers)) {
echo 'something is wrong';
} else {
echo 'nothing is wrong';
}
}
您可能也想检查垃圾邮件文件夹。 您是从localhost还是自己的网站发送的? 某些网络服务器软件如xampp不允许/执行?这个邮件功能默认。