与AJAX链接时,PHPMailer不起作用

时间:2016-09-17 18:41:49

标签: php jquery html ajax phpmailer

注意:我已阅读其他问题而且没有帮助。

所以我首先在我的localhost上使用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链接时,它只能在本地服务器上运行,但不能在现场运行。任何答案或建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

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