【问题标题】:Can't Send PHPMailer with Oauth Google无法使用 Oauth Google 发送 PHPMailer
【发布时间】:2017-08-04 07:25:05
【问题描述】:

我有一些问题。 我的代码

<?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 上搜索过,但我还没有找到解决方案

【问题讨论】:

  • 你跑composer install了吗?另外,您的require '../vendor/autoload.php' 在哪里?除非您有一个框架为您处理这个问题,否则必须将其包含在内。
  • 是的,我完成了 composer install

标签: php email oauth-2.0 google-oauth phpmailer


【解决方案1】:

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

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

我已将 cmets 添加到 Gmail OAuth2 tutorial on the PHPMailer wiki

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

【讨论】:

猜你喜欢
  • 2018-01-11
  • 2014-04-28
  • 1970-01-01
  • 2015-09-06
  • 2020-11-25
  • 2018-01-08
  • 1970-01-01
  • 2017-07-19
  • 2017-10-05
相关资源
最近更新 更多