简体   繁体   English

错误 - 200:使用 phpmailer 发送邮件时出现解析器错误

[英]Error - 200: parsererror on send of mail using phpmailer

I've just started using phpmailer to send mail instead of the basic mail function.我刚刚开始使用 phpmailer 发送邮件而不是基本的邮件功能。 I've added the phpmailer function to an ajax script to send a mail on a certain event.我已将 phpmailer 函数添加到 ajax 脚本中,以针对特定事件发送邮件。 The script works perfectly fine on its own, but as soon as I add the $mail->send();该脚本本身就可以正常工作,但是一旦我添加了$mail->send(); command, I get an error:命令,我收到一个错误:

"Error - 200: parsererror"

It does work - the mail gets sent, but it doesn't return correctly to the calling procedure.它确实有效 - 邮件被发送,但它没有正确返回到调用过程。 I've cut down my code a bit to remove extra validations and functions that aren't relevant to this particular issue.我已经减少了一些代码以删除与此特定问题无关的额外验证和函数。 The calling code is:调用代码为:

    jQuery('#save-supplier').click(function(e){
    e.preventDefault();
      
    $('#save-supplier').html('<i class="fa fa-cloud"></i> Saving...');
                                  
    var formData = new FormData($('#edit-supplier-form')[0]);
      
    $.ajax({
        type: "POST",
        url: "./ajax/ajaxSendphpMail.php",
        data: formData,
        contentType: false,
        processData:false,
        cache: false,
        dataType: 'json',
        success: function(data) {
            $(location).attr("href", "supplier.php?supplier_code=<?php echo $supplier_code; ?>");
        },
        error: function(data){
            var errorMessage = data.status + ': ' + data.statusText;
            alert('Error - ' + errorMessage);
            $('#save-supplier').html('<i class="fa fa-save mr-5"></i> Save Changes');
        }
    });
  
    return false;
});

}); });

And the script with the phpmailer command is:带有 phpmailer 命令的脚本是:

    ob_start();
session_start();
require '../inc/_global/db_con.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require '/var/www/vhosts/compleit.com/vendor/autoload.php';

try {
    $mail = new PHPMailer(true);

    //Server settings
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    $mail->Host       = 'compleit.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'smtp@compleit.com';
    $mail->Password   = 'cAp$xxxYY';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    //Recipients
    $mail->setFrom('myemail@mydomain.com');
    $mail->addAddress('gavinb@compleit.com');
    $mail->addReplyTo('donotreply@mydomain.com');

    // Content
    $mail->isHTML(true);
    $mail->Subject = 'test email';
    $mail->Body    = 'email body';
    $mail->AltBody = 'email body';

    $mail->send();
} catch (Exception $e) {
    // no exception reporting - no need to know if email succeeded or failed
}

Previously, I was sending mail through the standard mail command and I had no problem.以前,我通过标准邮件命令发送邮件,我没有问题。 But now that I've switched to phpmailer and I get the 200 parsererror.但是现在我已经切换到 phpmailer 并且我得到了 200 解析器错误。 The only way to stop that is to comment out the $mail->send() command.阻止这种情况的唯一方法是注释掉$mail->send()命令。

There are a couple of obvious problems here.这里有几个明显的问题。 PHPMailer does almost nothing until you actually call send() , which is why you're only seeing stuff happening then. PHPMailer 在您实际调用send()之前几乎什么都不做,这就是为什么您当时只能看到正在发生的事情。

Your JS is expecting valid JSON in responses, but you have SMTPDebug enabled, which will produce lots of output that's definitely not valid JSON, so set that to 0 or false .您的 JS 期望在响应中使用有效的 JSON,但您启用了SMTPDebug ,这将产生大量绝对不是有效 JSON 的输出,因此将其设置为0false This is what's causing your parser error, and it will be visible in your browser's dev tools if you look at the XHR requests it logs.这就是导致解析器错误的原因,如果您查看它记录的 XHR 请求,它将在浏览器的开发工具中可见。

You're also missing a content-type header in your response, so your script (if it's obeying the rules) will not parse the response.您的响应中还缺少内容类型标头,因此您的脚本(如果它遵守规则)将不会解析响应。 Add a line before you produce any JSON output:生成任何 JSON 输出之前添加一行:

header('Content-type: application/json');

In the case of a successful send, you're not echo ing anything.在成功发送的情况下,您不会echo任何内容。

In debug situations like this, debug the individual components first .在这种调试情况下,首先调试各个组件 Make sure your PHP code is working (both success and failure paths) before you try to use it from JS.您尝试从 JS 中使用它之前,请确保您的 PHP 代码正常工作(成功和失败路径)。 At the moment you're trying to debug your PHP code from JS, which is unnecessarily difficult.目前,您正在尝试从 JS 调试 PHP 代码,这是不必要的困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM