这是我的PHP邮件代码:
if ($_POST) {
$email=$_POST['email'];
$subject=$_POST['subject'];
$budget=$_POST['budget'];
$text=$_POST['text'];
$to="hello@mymail.com";
mail($to, "Piece of Cake = ".$subject, $text, "Budget:".$budget, "From:".$email );
}
这是我的AJAX电子邮件功能:
$(function (){
$('button#send_brief').click(function(){
var email = $('#brief_email');
var subject = $('#brief_type');
var budget = $('#brief_budget');
var text = $('#brief_comments');
var emailerData = 'email' + email +'&subject' + subject + '&budget' + budget+ '&text' + text;
if(text=='') {
$('#brief_email').css({'background-color':'red'});
}
else {
$.ajax({
type: "POST",
url:"emailer.php",
data: emailerData,
success: function(){
$('.success').fadeIn(1000);
setTimeout (function(){
$('.brief').slideUp({
duration: 1000,
easing: 'easeInSine'});}
,2000);
setTimeout (function(){
$('#brief').fadeIn();}
,3000);
setTimeout (function(){
$('.success').fadeOut(1000);}
,2000);
}
});
return false;}
});
});
这是我的表格:
<form method="POST" action="" >
<fieldset>
<label for="brief_email">Your email</label>
<input type="text" id="brief_email" placeholder="your contact email here"/>
etc ...
</fieldset>
</form>
<button id="send_brief">Send it</button>
首先,当我将所有字段留空时,它仍会显示成功屏幕。第二,即使输入有效数据,我也不会收到发送的电子邮件。
我已多次查看代码,对我来说似乎没问题。
请帮我找到我错的地方。
答案 0 :(得分:2)
你的问题出在AJAX构造中,你缺乏价值:val()
var email = $('#brief_email').val();
var subject = $('#brief_type').val();
var budget = $('#brief_budget').val();
var text = $('#brief_comments').val();
如果没有val()
,您将向PHP脚本发送一个对象而不是值。
vars构建你缺少=
var emailerData = 'email=' + email +'&subject=' + subject + '&budget=' + budget+ '&text=' + text;
您的mail()
构造也很糟糕,您无法发送任何其他值,而不是检查mail()。您检查$_POST
,$_POST
始终为真,您需要检查至少一个帖子值。
<?php
if (isset($_POST['email'])) {
$email=$_POST['email'];
$subject=$_POST['subject'];
$budget=$_POST['budget'];
$text=$_POST['text'];
$to = 'mail@example.com';
$subject = "Piece of Cake = ".$_POST['subject'];
$message = "New message from website
Email: $email
Budget: $budget
Comments: $text";
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
答案 1 :(得分:1)
您需要查看发送的电子邮件
if ($_POST) {
$email=$_POST['email'];
$subject=$_POST['subject'];
$budget=$_POST['budget'];
$text=$_POST['text'];
$to="hello@mymail.com";
if(mail($to, "Piece of Cake = ".$subject, $text, "Budget:".$budget, "From:".$email )) echo '1';
else echo '0';
}
检查ajax响应中的电子邮件发送响应
$(function (){
$('button#send_brief').click(function(){
var email = $('#brief_email');
var subject = $('#brief_type');
var budget = $('#brief_budget');
var text = $('#brief_comments');
var emailerData = 'email' + email +'&subject' + subject + '&budget' + budget+ '&text' + text;
if(text=='') {
$('#brief_email').css({'background-color':'red'});
}
else {
$.ajax({
type: "POST",
url:"emailer.php",
data: emailerData,
success: function(result){
if(result == 1){
$('.success').fadeIn(1000);
setTimeout (function(){
$('.brief').slideUp({
duration: 1000,
easing: 'easeInSine'});}
,2000);
setTimeout (function(){
$('#brief').fadeIn();}
,3000);
setTimeout (function(){
$('.success').fadeOut(1000);}
,2000);
}
}
});
return false;}
});
});
答案 2 :(得分:1)
您的电子邮件发送代码有两个问题:
mail($to, "Piece of Cake = ".$subject, $text, "Budget:".$budget, "From:".$email );
第一个问题是你没有检查返回值:
if (false === mail( ... )) { echo "Yelp! Mail not sent"; }
第二个问题是您的电子邮件标题应合并到第四个参数中:
mail($to, "Piece of Cake = ".$subject, $text, "Budget: $budget\r\nFrom: $email");
那是因为第五个参数适用于sendmail
,它不是电子邮件标题的另一个字段。