我正在尝试使用php表单和AJAX发送简单的电子邮件。但是,每次执行此操作时,代码都会抛出email could not be sent, please try again
错误。为什么会这样?
我还尝试alert
警报序列中的变量,但它不起作用。我检查了变量并且它们似乎有效,但似乎我忽略了一些东西。
Javascript / jQuery (在<head>
区块中)
<script type="text/javascript" src="js2/jquery-1.7.2.min.js"></script>
<script>
$("#form2").submit(function(e){
$(".error").remove();
$('#cancel2').click(function(){
return;
});
var hasError = false;
var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var nameVal = $("#name").val();
if (nameVal == '') {
$("#name").after('<span class="error">You forgot to enter the name.</span>');
hasError = true;
}
var emailToVal = $("#emailTo").val();
if (emailToVal == '') {
$("#emailTo").after('<span class="error">You forgot to enter the email address to send to.</span>');
hasError = true;
} else if(!emailReg.test(emailToVal)) {
$("#emailTo").after('<span class="error">Enter a valid email address to send to.</span>');
hasError = true;
}
var emailFromVal = $("#emailFrom").val();
if(emailFromVal == '') {
$("#emailFrom").after('<span class="error">You forgot to enter the email address to send from.</span>');
hasError = true;
} else if (!emailReg.test(emailFromVal)) {
$("#emailFrom").after('<span class="error">Enter a valid email address to send from.</span>');
hasError = true;
}
if (hasError == false){
$('.navLinks').hide();
$('.navLinks:before').hide();
$('.navLinks:after').hide();
s
$.ajax({
"type":"POST",
"url":"sendemail.php",
"data": { name: nameVal, emailTo: emailToVal, emailFrom: emailFromVal },
"dataType":'json',
"success":function(){
//$('#sumbit1').after('<img src="/img/wait.gif" alt="wait" />');
alert('Your email has been sent! For faster answer please call us at: (403) 454-5526');
$('.navLinks').hide();
},
"error": function (){
alert('Your email could not be sent, please try again.');
}
});
}
e.preventDefault();
return;
});
</script>
HTML
<form name="send-to-friend" id="form2" class="form" method="post" action="sendemail.php">
<label for="name" class="blockItem">Your Name</label>
<input type="text" id="name" class="blockItem" name="your name" maxlength="60" size="35"/>
<label for="emailFrom" class="blockItem">Your Email Address</label>
<input type="text" id="emailFrom" class="blockItem" name="your email" maxlength="60" size="35" />
<label for="emailTo" class="blockItem">Your Friend's E-mail Address</label>
<input type="text" id="emailTo" class="blockItem" name="your friends email" maxlength="60" size="35" />
<input class="button" id="submitt2" type="submit" value="SUBMIT" />
</form>
PHP
<?php
$mailTo = $_POST['emailToVal'];
$mailFrom = $_POST['emailFromVal'];
$name = $_POST['nameVal'];
$message = 'I found this company recommended for residential and office cleaning in Calgary. Their website is FineMaid "dot" com (replace "dot", of course in browser\s address bar). Cheers!';
$subject = $name . ' recommends Fine Maid';
$send = @mail($mailTo, $subject, $message, "From: ".$mailFrom);
?>
答案 0 :(得分:0)
您在PHP中访问的参数不存在,应该是:
$mailTo = $_POST['emailTo'];
$mailFrom = $_POST['emailFrom'];
$name = $_POST['name'];
但是,当您使用JSON作为dataType时,应返回JSON。
此外:当PHP脚本返回有效的JSON时,没有检查邮件是否已经发送,当前jQuery将报告成功(它没有,因为响应至少是一个空格)或者缺少POST变量的注意事项,而不是JSON)
将它放在脚本的末尾:
if(!$send)
{
header("HTTP/1.0 500 Internal Server Error");
}
else
{
die(json_encode(array('msg'=>'Your email has been sent!
For faster answer please call us at: (403) 454-5526')));
}
...当$ send为true(邮件已被发送)时,脚本将返回JSON,否则响应将具有HTTP状态代码500,这将触发错误回调