我正在使用Xampp,我想整合通过PHP发送Gmail。我看到每个人都推荐PHP Mailer,所以我下载Composer并安装phpmailer composer require phpmailer/phpmailer
。之后我安装google oauth2:composer require league/oauth2-google
。
我查看this链接使用:授权代码流程。
谷歌API全部设置(我有APP-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是这样的:
{
"require": {
"phpmailer/phpmailer": "^6.0",
"league/oauth2-client": "^2.3",
"stevenmaguire/oauth2-microsoft": "^2.2"
}
}
很抱歉,但我是新手,无法找到原因.php页面找不到Class。
这是get_oauth_token.php
<?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();
}
?>
答案 0 :(得分:2)
我刚刚做同样但没有作曲家。首先,它没有处理一些错误。当我在寻找故障排除的解决方案,并尝试编辑一些行时,最后它现在正在工作。结果如下:
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
我的代码:
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;
}
我还在xampp / php文件夹中禁用/评论与php.ini上的电子邮件相关的所有行并重新加载xampp。
从https://github.com/PHPMailer/PHPMailer
获取最新代码希望它会有所帮助。