PHPMalier - SMTP 错误:无法进行身份验证

时间:2021-03-23 14:47:12

标签: php phpmailer

你能试试代码吗? 猜我哪里错了

    ```
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    require 'vendor/autoload.php';

    $mail = new PHPMailer(true);
    try {
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true


            )
        );

        $mail->SMTPDebug = 2;
        $mail->isSMTP();
        $mail->Host = "smtp.gmail.com";
        $mail->SMTPAuth = true;
        $mail->Username = "********@gmail.com";
        $mail->Password = "********";
        $mail->SMTPSecure = "tls";
        $mail->Port = 587;

        $mail->setFrom("********@gmail.com", "My name");
        $mail->addAddress("********@gmail.com");
        $mail->isHTML(true);
        $mail->Subject = "Mailer Test";
        $mail->Body = "This is a Test";

        $mail->send();
        echo "Ok";
    } catch (Exception $e) {
        echo "No";
    }
    ```

有两个重复的错误在输出中写入了两次: SMTP 错误:无法验证。 SMTP 错误:无法验证。 有什么问题? 能不能写修改代码

1 个答案:

答案 0 :(得分:0)

正如我在上面的评论中提到的。如果我通过 Composer 安装 PHPmailer,它会告诉我我还需要安装其他软件包以进行正确的身份验证。我今天再次为一个新项目安装了 PHPmailer,所以我复制了它显示给我的行。

phpmailer/phpmailer suggests installing hayageek/oauth2-yahoo (Needed for Yahoo XOAUTH2 authentication)
phpmailer/phpmailer suggests installing league/oauth2-google (Needed for Google XOAUTH2 authentication)
phpmailer/phpmailer suggests installing psr/log (For optional PSR-3 debug logging)
phpmailer/phpmailer suggests installing stevenmaguire/oauth2-microsoft (Needed for Microsoft XOAUTH2 authentication)
phpmailer/phpmailer suggests installing symfony/polyfill-mbstring (To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2))