无法通过Oauth Google

时间:2017-08-03 19:35:02

标签: php email oauth-2.0 google-oauth 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上搜索但我还没有找到解决方案

1 个答案:

答案 0 :(得分:0)

查看PHPMailer's composer.json file - 它包含OAuth类作为建议,因为它不是必需的,并且不适用于PHPMailer支持的旧PHP版本,因此它可以'默认情况下启用。

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

我已将此效果的评论添加到Gmail OAuth2 tutorial on the PHPMailer wiki

虽然我在这里,但我建议您不要使用PHPMailer 5.2实现此功能,而是直接转到PHPMailer 6.0,它对OAuth2有更好的支持。