【问题标题】:PHPMailer when linked with AJAX doesnt work与 AJAX 链接时的 PHPMailer 不起作用
【发布时间】:2017-01-25 19:14:48
【问题描述】:

注意:我已阅读其他问题,但它们没有帮助。

所以我首先使用 ajax 和 PHPMailer 在我的 localhost 上进行了测试,当我收到电子邮件时它运行良好,并且使用 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 系统都倾向于需要 JSON 格式的返回值,而您并没有这样做。您也没有使用 PHPMailer 的错误报告功能,这并不奇怪,这意味着您不知道发生了什么。
  • @Synchro 我确实启用了错误报告,但我无法准确查看它们,不在控制台、页面或任何地方。那是因为它是一个外部文件吗?并且不起作用我的意思是它说它发送失败并且不发送。
  • 如果您可以看到它说发送失败,您可以看到错误输出。试着简单地说die("There was an error sending the email.".$mail-&gt;ErrorInfo);作为开始。
  • @Synchro 好的,谢谢,让我试试
  • @Synchro 所以我这样做了,在 jQuery 中做了 alert(data) 但由于某种原因它返回空白。我做错了什么吗?当我说它说发送失败时,我的意思是如果它没有返回 true,我就是用“unsent.html”写的。

标签: php jquery html ajax phpmailer


【解决方案1】:

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

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

【讨论】:

  • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
  • 哦,好的,当然。只是提供建议以获取系统报告的错误。下次会用问题cmets。
猜你喜欢
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
  • 2018-07-05
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
相关资源
最近更新 更多