PHPMailer与AJAX链接时不起作用


PHPMailer when linked with AJAX doesnt work

注意:我已经看了其他问题,他们没有帮助。

所以我首先在我的本地主机上测试了ajax和PHPMailer,它工作得很好,因为我收到了电子邮件,用ajax它说电子邮件发送。

当我上传到一个活的网站,它停止工作。我确信连接是可靠的,因为当我去掉AJAX,把它作为PHP页面时,它可以工作。但是,当它是一个带有AJAX的外部文件时,它不会(在一个实时网站上)

下面是AJAX代码:

<script>
    var ajax = {
        isSubmiting: false,
        send: function() {
            if(ajax.isSubmiting == false) {
                ajax.isSubmiting = true;
                var userName = $("input[name=name]").val();
                var userEmail = $("input[name=email]").val();
                var userComments = $("textarea").val();
                var currentBusiness = $("input[name=business").val();
                var currentWebsite = $("input[name=website]").val();
                    ajax.SetText("Sending...");
                    $.post("sendmail.php", {
                        name: userName, email: userEmail, comments: userComments, business: currentBusiness, website: currentWebsite
                    }, function(data) {
                        if(data == "true") {
                            ajax.SetText("Sent!");  
                            $.get("sent.html", function(sentData){
                                $("#content").html(sentData);
                            });
                        } else {
                            ajax.SetText("Send mail");
                            $.get("unsent.html", function(sentData){
                                $("#content").html(sentData);
                            });
                            console.log();
                        }
                        ajax.isSubmiting = false;
                    });
            }
            else alert("You can only send 1 email at a time");
        },
        SetText: function(text) {
            $("input[type=button]").val(text);
        }
    }
</script>
下面是PHPMailer脚本(sendmail.php):
<?php
    $result = "";
    $error = "";
    if(count($_POST) > 0) {
            $message=
            'Full Name: '.$_POST['name'].'<br />
            Email:  '.$_POST['email'].'<br />
            Message: '.$_POST['comments'].'<br />
            Current Website: '.$_POST['website'].'<br />
            Business Name: '.$_POST['business'].'<br />
            ';
            include "phpmailer/class.smtp.php";
            include("includes/class.phpmailer.php");
            $mail = new PHPMailer();
            //Tell PHPMailer to use SMTP
            $mail->isSMTP();
            //Enable SMTP debugging
            // 0 = off (for production use)
            // 1 = client messages
            // 2 = client and server messages
            $mail->Host = localhost;
            //Set who the message is to be sent from
            $mail->setFrom('info@coherenthub.com', 'Coherent');
            //Set an alternative reply-to address
            //$mail->addReplyTo('replyto@example.com', 'First Last');
            //Set who the message is to be sent to
            $mail->addAddress('coherenthub@gmail.com', 'Coherent');
            //Set the subject line
            $mail->Subject = 'Contact Form';
            //Read an HTML message body from an external file, convert referenced images to embedded,
            //convert HTML into a basic plain-text alternative body
            $mail->MsgHTML($message); 
            //Replace the plain text body with one created manually
            $mail->AltBody = 'This is a plain-text message body';
            if (!$mail->send()) {
                die("There was an error sending the email.");   
            } else {
                die("true");    
            }

        }
?>

有谁知道为什么这不会被说出来吗?重申一下,在一个没有ajax的PHP页面上,加上脚本,它可以在本地服务器上正常工作。但是,当我把它放在一个外部文件和AJAX链接,它只在本地服务器工作,而不是在现场。任何答案或建议将非常感激。

您已经在脚本中使用了相对路径。当从Ajax调用时,确保路径被转换为正确的路径。

您可以检查控制台检查Ajax调用是否正常工作,然后检查服务器错误日志以验证没有php错误。