1

我对 gmail 身份验证有一个小问题,尝试通过 OAuth2 接收到的令牌进行连接。

我把我的代码放在下面。

try {
        $xOAuthRequest = base64_encode('user=' . $account->getSmtpServer()->getLogin() . "\1auth=Bearer " . $account->getOAuthToken()->getToken() . "\1\1");
        $transport = new TransportSmtp();

        $options = new SmtpOptions([
            'host' => 'smtp.gmail.com',
            //'port' => 587,
            'connection_class' => 'smtp',
            'connection_config' => [
                'username' => $account->getSmtpServer()->getLogin(),
                //'password' => $account->getSmtpServer()->getPassword(),
                'ssl' => 'tls',
                'port' => 587,
                'auth' => 'xoauth',
                'xoauth_request' => $xOAuthRequest
            ]
        ]);

        $transport->setOptions($options);
        $this->setTransport($transport);

    } catch (Exception $exception){
        dump($exception);
    }

代码返回:

5.7.0 Authentication Required. Learn more at 5.7.0 https://support.google.com/mail/?p=WantAuthError v23sm426024ljg.122 - gsmtp

我还尝试了使用 SSL 和端口 465 并且未注释密码。

我究竟做错了什么?

4

0 回答 0