php发送电子邮件

时间:2010-08-29 19:41:02

标签: php

mailer.php

<?php
if(isset($_POST['submit'])) {

$to = "abc@gmail.com";
$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几乎一无所知,我做错了什么?

2 个答案:

答案 0 :(得分:5)

您的PHP脚本永远不会仅打印1打印01Data has been submitted to...,因此(data == "1")永远不会为真。


btw:您的脚本至少可以使用mail()的返回值来判断它是否表示成功或失败。

答案 1 :(得分:0)

首先,检查服务器的邮件设置。如果您的脚本驻留在您不管理的服务器上,最简单的方法是创建一个包含以下内容的简单PHP文件:

<?php 
  mail("youraccount@example.com","test","works"); <br>
?>

运行它并检查您的电子邮件。