无法通过Oauth Google发送PHPMailer [英] Can't Send PHPMailer with Oauth Google

查看:93
本文介绍了无法通过Oauth Google发送PHPMailer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题. 我的代码

<?php
require 'PHPMailer/PHPMailerAutoload.php';
$userEmail = "mail@mail.org";
$clientId = "xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
$clientSecret = "xxxxxxxx-xxxxxxxxx";
$token = "1/xxxxxxxx-xxxxxxxx-5EmWcSmuvnRbJs";

$mailer = new PHPMailerOAuth;
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->AuthType = 'XOAUTH2';
$mailer->oauthUserEmail = $userEmail;
$mailer->oauthClientId = $clientId;
$mailer->oauthClientSecret = $clientSecret;
$mailer->oauthRefreshToken = $token;
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;

$mailer->setFrom("xxxxx@xxxxxxx.org");
$mailer->addAddress("xxxxxx@gmail.com");

$mailer->Subject = "Subject";
$mailer->Body = "Message";

if (! $mailer->send())
    throw new RuntimeException('Mail submission failed! ' . $mailer->ErrorInfo);

这个错误

Fatal error: Uncaught Error: Class 'League\OAuth2\Client\Provider\Google' not found in /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php:54 Stack trace: #0 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php(67): PHPMailerOAuthGoogle->getProvider() #1 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php(74): PHPMailerOAuthGoogle->getToken() #2 /opt/lampp/htdocs/mailer/PHPMailer/class.smtp.php(470): PHPMailerOAuthGoogle->getOauth64() #3 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauth.php(174): SMTP->authenticate('', '', 'XOAUTH2', '', '', Object(PHPMailerOAuthGoogle)) #4 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1540): PHPMailerOAuth->smtpConnect(Array) #5 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1335): PHPMailer->smtpSend('Date: Thu, 3 Au...', 'Message\n') #6 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1213): PHPMailer->postSend() #7 /opt/lampp/htdocs/mailer/index.php(27): PHPMailer->send() #8 {main} thrown in /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php on line 54

我不知道我的错误是什么,我只想使用带有Oauth2.0的google smtp和PHPmailer创建邮件,我已经在google上搜索了,但是还没有找到解决方法

i don't know what my mistake, i just want to create mail with PHPmailer using google smtp with Oauth2.0, I've searched on google but I have not found the solution yet

推荐答案

查看

Look at PHPMailer's composer.json file - it includes the OAuth class as a suggestion, because it's not a requirement, and won't work on older PHP versions that PHPMailer supports, so it can't be enabled by default.

您需要将其添加到自己的composer.json文件(与加载PHPMailer相同的文件).

You need to add it to your own composer.json file (the same one you load PHPMailer with).

我已为此 Gmail OAuth2教程添加了有关此效果的评论在PHPMailer Wiki上.

虽然我在这里,但我建议您不要使用PHPMailer 5.2实现此功能,而是直接转到

While I'm here, I suggest you don't implement this using PHPMailer 5.2, but go directly to PHPMailer 6.0 which has much better support for OAuth2.

这篇关于无法通过Oauth Google发送PHPMailer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆