PHPMailer响应

eri*_*ali 0 php phpmailer

我正在使用phpmailer发送具有以下功能的电子邮件:

function send_email($address, $message){
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "name@gmail.com";
$mail->Password = "password";
$mail->SMTPSecure = "tls";
$mail->Port = 587;

$mail->From = "name@gmail.com";
$mail->FromName = "Name";
$mail->addAddress($address, "Cust. name");

$mail->isHTML(true);

$mail->Subject = "Nueva clave";
$mail->Body = "<i>".$message."</i>";
$mail->AltBody = "This is yout password.";

if($mail->send())
{
    return true;
}else{
    return false;
}
}
Run Code Online (Sandbox Code Playgroud)

在我的php文件中,我以这种方式设置了响应标头: header('Content-Type: application/json');在需要进行处理之后,我以这种方式处理电子邮件:

 if(send_email('customer@gmail.com', $message))
        {
            array_push($response, 'email sent');
            array_push($response, $new_password);
            echo json_encode(array('errors'=>$errors, 'response' => $response));
        }
        else
        {
            array_push($errors, 'email not send');
            array_push($errors, $new_password);
            echo json_encode(array('errors'=>$errors, 'response' => $response));
        }
Run Code Online (Sandbox Code Playgroud)

通常,我希望服务器发出如下响应:Object [errors [],response []]就像我做几次一样,但是我收到的却是答案 JSON响应

为什么得到此响应,我是否需要其他未设置的配置。谢谢

Syn*_*hro 5

您将在响应中包含调试输出,该输出是无效的JSON。只需执行以下操作即可将其关闭:

$mail->SMTPDebug = 0;
Run Code Online (Sandbox Code Playgroud)