首页
学习
活动
专区
圈层
工具
发布

oauth2
EN

Stack Overflow用户
提问于 2018-05-24 10:35:46
回答 1查看 1.8K关注 0票数 1

我正在使用Xampp,我想集成通过PHP发送Gmail。我看到每个人都推荐phpmailer,所以我下载Composer并安装phpmailer composer require phpmailer/phpmailer。之后,我安装了google oauth2:composer require league/oauth2-google

我查看要使用的链接:授权代码流。

Google都设置好了(我有应用程序id和秘密)

当我运行get_oauth_token.php时,会得到错误:Uncaught Error: Class 'League\OAuth2\Client\Provider\Google' not found in C:\xampp\htdocs\telesales\gentelella-master\production\PHPMailer\get_oauth_token.php

我的composer.json是:

代码语言:javascript
复制
{
    "require": {
        "phpmailer/phpmailer": "^6.0",
        "league/oauth2-client": "^2.3",
        "stevenmaguire/oauth2-microsoft": "^2.2"
    }
}

对不起,我是新来的,我不知道为什么.php页面找不到类。

这是get_oauth_token.php

代码语言:javascript
复制
<?php
$provider = new League\OAuth2\Client\Provider\Google([
    'clientId'     => '{google-app-id}',
    'clientSecret' => '{google-app-secret}',
    'redirectUri'  => 'https://example.com/callback-url',
    'hostedDomain' => 'example.com', // optional; used to restrict access to users on your G Suite/Google Apps for Business accounts
]);

if (!empty($_GET['error'])) {

    // Got an error, probably user denied access
    exit('Got error: ' . htmlspecialchars($_GET['error'], ENT_QUOTES, 'UTF-8'));

} elseif (empty($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: ' . $authUrl);
    exit;

} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    // State is invalid, possible CSRF attack in progress
    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the owner details
        $ownerDetails = $provider->getResourceOwner($token);

        // Use these details to create a new profile
        printf('Hello %s!', $ownerDetails->getFirstName());

    } catch (Exception $e) {

        // Failed to get user details
        exit('Something went wrong: ' . $e->getMessage());

    }

    // Use this to interact with an API on the users behalf
    echo $token->getToken();

    // Use this to get a new access token if the old one expires
    echo $token->getRefreshToken();

    // Number of seconds until the access token will expire, and need refreshing
    echo $token->getExpires();
}
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-27 02:58:09

我只是做了同样的,但没有作曲家。首先,它没有处理一些错误。当我搜索解决方案的疑难解答,并试图编辑一些行,最后,它现在开始工作。有了这个结果:

代码语言:javascript
复制
            2018-05-27 02:41:44 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:44 CLIENT -> SERVER: EHLO mybasic.local
2018-05-27 02:41:44 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.213.130.29]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-05-27 02:41:44 CLIENT -> SERVER: STARTTLS
2018-05-27 02:41:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2018-05-27 02:41:45 CLIENT -> SERVER: EHLO myweb.local
2018-05-27 02:41:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.213.130.29]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-05-27 02:41:45 CLIENT -> SERVER: AUTH LOGIN
2018-05-27 02:41:45 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2018-05-27 02:41:45 CLIENT -> SERVER: <credentials hidden>
2018-05-27 02:41:45 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2018-05-27 02:41:45 CLIENT -> SERVER: <credentials hidden>
2018-05-27 02:41:46 SERVER -> CLIENT: 235 2.7.0 Accepted
2018-05-27 02:41:46 CLIENT -> SERVER: MAIL FROM:<your_email@gmail.com>
2018-05-27 02:41:46 SERVER -> CLIENT: 250 2.1.0 OK q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:46 CLIENT -> SERVER: RCPT TO:<your_email_or_other@gmail.com>
2018-05-27 02:41:46 SERVER -> CLIENT: 250 2.1.5 OK q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:46 CLIENT -> SERVER: DATA
2018-05-27 02:41:46 SERVER -> CLIENT: 354 Go ahead q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:46 CLIENT -> SERVER: Date: Sun, 27 May 2018 04:41:44 +0200
2018-05-27 02:41:46 CLIENT -> SERVER: To: Test System <your_email@gmail.com>
2018-05-27 02:41:46 CLIENT -> SERVER: From: Test System <your_email@gmail.com>
2018-05-27 02:41:46 CLIENT -> SERVER: Subject: Here is the subject
2018-05-27 02:41:46 CLIENT -> SERVER: Message-ID: <ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk@mybasic.local>
2018-05-27 02:41:46 CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
2018-05-27 02:41:46 CLIENT -> SERVER: MIME-Version: 1.0
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Type: multipart/alternative;
2018-05-27 02:41:46 CLIENT -> SERVER: boundary="b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk"
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: This is a multi-part message in MIME format.
2018-05-27 02:41:46 CLIENT -> SERVER: --b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: This is the body in plain text for non-HTML mail clients
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: --b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk
2018-05-27 02:41:46 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: This is the HTML message body <b>in bold!</b>
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: --b1_ECAniOwmuQcv1zYojWJ3VxZx8xCqVgayTymBYoKlKk--
2018-05-27 02:41:46 CLIENT -> SERVER:
2018-05-27 02:41:46 CLIENT -> SERVER: .
2018-05-27 02:41:48 SERVER -> CLIENT: 250 2.0.0 OK 1527388939 q126-v6sm37491953pga.79 - gsmtp
2018-05-27 02:41:48 CLIENT -> SERVER: QUIT
2018-05-27 02:41:48 SERVER -> CLIENT: 221 2.0.0 closing connection q126-v6sm37491953pga.79 - gsmtp
Message has been sent 

我的代码:

代码语言:javascript
复制
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php'; // make sure the folder is right
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

//Load Composer's autoloader
//require 'vendor/autoload.php'; // if using composer

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';                     // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
     $mail->Username = 'your_email@gmail.com';                 // SMTP username
     $mail->Password = 'your_email_password';  
    $mail->SMTPSecure = 'tsl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

     $mail->setFrom('your_email@gmail.com', 'Test System');
     $mail->addAddress('your_email_or_other@gmail.com', 'Test System');  
    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

我还禁用了/注释与php.ini文件夹xampp/php中的电子邮件相关的所有行,并重新加载xampp。

从:https://github.com/PHPMailer/PHPMailer获取最新代码

希望能帮上忙。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50507175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档