PHP发送电子邮件
问题描述:
mailer.phpPHP发送电子邮件
<?php
if(isset($_POST['submit'])) {
$to = "[email protected]";
$subject = "Contact via website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "1";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "0";
}
?>
jQuery代码
$('.submit').click(function(){
$('span.msg').css({'visibility': 'visible'}).text('Sending...');
$.post("mailer.php", $(".contactPage").serialize(),
function(data){
$('span.msg').text((data == "1") ? 'Your Message has been received. Thank you' : "Could not send your message at this time").show();
});
return false;
});
我总是得到
Could not send your message at this time
我在PHP几乎没有知识,什么我做错了吗?
答
你的php脚本从不打印只是1
它打印0
或1Data has been submitted to...
因此(data == "1")
永远不会是真的。
顺便说一句:您的脚本可以用最少的mail()
返回值来决定是否它标志着成功或失败。
答
首先,检查服务器的邮件设置。如果你的脚本驻留在服务器上,你不管理上,最简单的方法就是做这一情况,内容如下一个简单的PHP文件:
<?php
mail("[email protected]","test","works"); <br>
?>
运行,并检查你的电子邮件。
+0
是的,它工作... – coure2011 2010-08-29 23:05:29
该怎么做? – coure2011 2010-08-29 20:03:47
我建议您打印{成功:true}而不是1或0。 在Jquery中使用一个JSON库或简单地使用(虽然不推荐) function(data){response = eval(data); if(response.success){alert(“Msg sent”); }} – 2010-08-29 20:26:55
任何代码片段? – coure2011 2010-08-29 22:32:25