php发送电子邮件

cou*_*011 0 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";
}
?>
Run Code Online (Sandbox Code Playgroud)

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;    
        });
Run Code Online (Sandbox Code Playgroud)

我总是得到

Could not send your message at this time
Run Code Online (Sandbox Code Playgroud)

我对php几乎一无所知,我做错了什么?

Vol*_*erK 5

你的PHP脚本永远不会打印1它打印0,1Data has been submitted to...因此(data == "1")永远不会是真的.


btw:你的脚本至少可以使用返回值mail()来决定它是成功还是失败.