繁体   English   中英

SMTP 错误无法验证和 SMTP 错误无法连接到服务器(0)

[英]SMTP Error could not authenticate and SMTP Error failed to connect to server(0)

使用最新的 PHPMailer 版本时,我收到此 SMTP 错误:

SMTP Error: Could not authenticate.

我尝试使用 SSL 而不是端口号为 465 的 TLS。OpenSSL 已经加载到配置文件中,我查看了故障排除指南,这些都没有影响,我的代码到底出了什么问题? 我不确定的一件事是将 OAuth2 提供程序 class 包括在内:

use League\OAuth2\Client\Provider\Google;

没有那个文件路径,我应该改变这个吗? 另外,当我将加密更改为 $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; 现在我得到了这个:SMTP 错误:无法连接到服务器:(0)。

这是代码:

    class gmail_xoauth

    {
        public static function sendMail($subject,$body,$address)
        {


    $mail = new PHPMailer();
    $mail->isSMTP();

    $mail->SMTPDebug = SMTP::DEBUG_CLIENT;
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->SMTPAuth = true;
    $mail->AuthType = 'XOAUTH2';

    //Fill in authentication details here
    //Either the gmail account owner, or the user that gave consent
    $email = 'myemail@gmail.com';
    $clientId = 'clientid';
    $clientSecret = 'clientSecret';
    //Obtained by configuring and running get_oauth_token.php
    //after setting up an app in Google Developer Console.
    $refreshToken = 'refreshToken';
    //Create a new OAuth2 provider instance
    $provider = new Google(
        [
            'clientId' => $clientId,
            'clientSecret' => $clientSecret,
        ]
    );
    //Pass the OAuth provider instance to PHPMailer
    $mail->setOAuth(
        new OAuth(
            [
                'provider' => $provider,
                'clientId' => $clientId,
                'clientSecret' => $clientSecret,
                'refreshToken' => $refreshToken,
                'username' => $email,
            ]
        )
    );
    //Set who the message is to be sent from
    //For gmail, this generally needs to be the same as the user you logged in as
    $mail->setFrom($email, 'me');
    //Set who the message is to be sent to
    $mail->addAddress($address, 'John Doe');
    //Set the subject line
    $mail->Subject = $subject;
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->CharSet = PHPMailer::CHARSET_UTF8;
    $mail->msgHTML(file_get_contents('PHPMailer/PHPMailer-master/examples/contentsutf8.html'), __DIR__);
    //Replace the plain text body with one created manually
    $mail->AltBody = 'This is a plain-text message body';
    //Attach an image file

    $mail->addAttachment('PHPMailer/PHPMailer-master/examples/images/phpmailer_mini.png');
    //send the message, check for errors
    if (!$mail->send()) {
        echo 'Mailer Error: '. $mail->ErrorInfo;
    } else {
        echo 'Message sent!';
    }
    }
    }

This is the full error

    SERVER -> CLIENT: 220 smtp.gmail.com ESMTP w67sm7347917yww.16 - gsmtp
    CLIENT -> SERVER: EHLO localhost
    SERVER -> CLIENT: 250-smtp.gmail.com at your service, [108.203.6.59]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
    CLIENT -> SERVER: STARTTLS
    SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
    CLIENT -> SERVER: EHLO localhost
    SERVER -> CLIENT: 250-smtp.gmail.com at your service, [108.203.6.59]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
    SMTP Error: Could not authenticate.
    CLIENT -> SERVER: QUIT
    SERVER -> CLIENT: 221 2.0.0 closing connection w67sm7347917yww.16 - gsmtp
    SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
    Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/TroubleshootingEmail sent!

暂无
暂无

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

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