PHP邮件函数发送电子邮件,但AJAX函数出错

PHP mail function sends out the email, but the AJAX function errors out

本文关键字:函数 AJAX 出错 电子邮件 PHP      更新时间:2023-09-26

我在PHP上使用"mail",它成功地发送了电子邮件。然而,我在AJAX的成功/错误函数上遇到了一个错误。我不断收到"电子邮件失败"的警报。提前谢谢。

PHP

<?php
    $emailTo = "test@test.com";
    $emailSubject = "Email Test";
    $emailBody = "Check Email";
    if(mail($emailTo, $emailSubject, $emailBody))
        echo "success";
    else
        echo "fail";
?>

AJAX

$.ajax({
    url:'email_data.php',
    type:'post',
    data:$('#postForm').serialize(),
    success:function(res)
    {
        if(res=="success")
            alert("Email Sent");
        else
            alert("Email Not Sent");
    },
    error:function()
    {
        alert("Email Failed");
    }
});

代码:

success:function(res)
{
    if(res=="successful")
        alert("Email Sent");

应该是:

success:function(res)
{
    if(res=="success")
        alert("Email Sent");

这是因为您在PHP文件中echo成功了。