Code cleanup, see #675
This commit is contained in:
parent
3a41f91fe2
commit
739127f8e9
|
|
@ -1,23 +1,17 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This example shows settings to use when sending via Google's Gmail servers.
|
||||
*/
|
||||
|
||||
namespace PHPMailer\PHPMailer;
|
||||
|
||||
// Give alias to the League Provider Classes that may be used
|
||||
// Alias the League OAuth2 provider class
|
||||
use League\OAuth2\Client\Provider\Google as Google;
|
||||
use Stevenmaguire\OAuth2\Client\Provider\Microsoft as Microsoft;
|
||||
use Hayageek\OAuth2\Client\Provider\Yahoo as Yahoo;
|
||||
|
||||
|
||||
//SMTP needs accurate times, and the PHP time zone MUST be set
|
||||
//This should be done in your php.ini, but this is how to do it if you don't have access to that
|
||||
date_default_timezone_set('Etc/UTC');
|
||||
|
||||
require '../vendor/autoload.php';
|
||||
|
||||
//Load dependencies from composer
|
||||
//If this causes an error, run 'composer install'
|
||||
require '../vendor/autoload.php';
|
||||
|
|
@ -32,7 +26,7 @@ $mail->isSMTP();
|
|||
// 0 = off (for production use)
|
||||
// 1 = client messages
|
||||
// 2 = client and server messages
|
||||
$mail->SMTPDebug = 0;
|
||||
$mail->SMTPDebug = 2;
|
||||
|
||||
//Ask for HTML-friendly debug output
|
||||
$mail->Debugoutput = 'html';
|
||||
|
|
@ -62,22 +56,20 @@ $clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
|
|||
// eg: http://localhost/phpmail/get_oauth_token.php
|
||||
$refreshToken = 'RANDOMCHARS-----DWxgOvPT003r-yFUV49TQYag7_Aod7y0';
|
||||
|
||||
//Change the Class Name depending on the Provider Used
|
||||
if (!isset($provider)) {
|
||||
$provider = new Google([
|
||||
'clientId' => $clientId,
|
||||
'clientSecret' => $clientSecret
|
||||
]);
|
||||
}
|
||||
|
||||
$mail->setOAuth(new OAuth(
|
||||
[
|
||||
'provider' => $provider,
|
||||
$provider = new Google([
|
||||
'clientId' => $clientId,
|
||||
'clientSecret' => $clientSecret,
|
||||
'refreshToken' => $refreshToken,
|
||||
'userName' => $email]
|
||||
));
|
||||
'clientSecret' => $clientSecret
|
||||
]);
|
||||
|
||||
$mail->setOAuth(
|
||||
new OAuth([
|
||||
'provider' => $provider,
|
||||
'clientId' => $clientId,
|
||||
'clientSecret' => $clientSecret,
|
||||
'refreshToken' => $refreshToken,
|
||||
'userName' => $email
|
||||
])
|
||||
);
|
||||
|
||||
//Set who the message is to be sent from
|
||||
//For gmail, this generally needs to be the same as the user you logged in as
|
||||
|
|
@ -99,14 +91,6 @@ $mail->AltBody = 'This is a plain-text message body';
|
|||
//Attach an image file
|
||||
$mail->addAttachment('images/phpmailer_mini.png');
|
||||
|
||||
$mail->SMTPOptions = array(
|
||||
'ssl' => array(
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false,
|
||||
'allow_self_signed' => true
|
||||
)
|
||||
);
|
||||
|
||||
//send the message, check for errors
|
||||
if (!$mail->send()) {
|
||||
echo "Mailer Error: " . $mail->ErrorInfo;
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@
|
|||
namespace PHPMailer\PHPMailer;
|
||||
|
||||
// Aliases for League Provider Classes that may be used
|
||||
// Make sure you have added these to your composer.json and run `composer install`
|
||||
use League\OAuth2\Client\Provider\Google as Google;
|
||||
use Stevenmaguire\OAuth2\Client\Provider\Microsoft as Microsoft;
|
||||
use Hayageek\OAuth2\Client\Provider\Yahoo as Yahoo;
|
||||
|
|
@ -60,7 +61,7 @@ if (array_key_exists('provider', $_GET)) {
|
|||
$providerName = $_SESSION['provider'];
|
||||
}
|
||||
if (!in_array($providerName, ['Google', 'Microsoft', 'Yahoo'])) {
|
||||
exit("Only Google, Microsoft and Yahoo OAuth2 providers are currently supported.");
|
||||
exit('Only Google, Microsoft and Yahoo OAuth2 providers are currently supported in this script.');
|
||||
}
|
||||
|
||||
//These details obtained are by setting up app in Google developer console.
|
||||
|
|
@ -72,7 +73,7 @@ $clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
|
|||
$redirectUri = isset($_SERVER['HTTPS']) ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
|
||||
//$redirectUri = 'http://localhost/PHPMailer/redirect';
|
||||
|
||||
$provider = new Google(
|
||||
$provider = new $providerName(
|
||||
[
|
||||
'clientId' => $clientId,
|
||||
'clientSecret' => $clientSecret,
|
||||
|
|
@ -81,21 +82,17 @@ $provider = new Google(
|
|||
]
|
||||
);
|
||||
|
||||
// Define scopes as a array here.
|
||||
// Set OAuth options
|
||||
$options = [
|
||||
'scope' => [
|
||||
'https://mail.google.com/'
|
||||
]
|
||||
];
|
||||
|
||||
|
||||
if (!isset($_GET['code'])) {
|
||||
|
||||
// If we don't have an authorization code then get one
|
||||
$authUrl = $provider->getAuthorizationUrl($options);
|
||||
|
||||
$_SESSION['oauth2state'] = $provider->getState();
|
||||
|
||||
header('Location: ' . $authUrl);
|
||||
exit;
|
||||
// Check given state against previously stored one to mitigate CSRF attack
|
||||
|
|
@ -105,13 +102,12 @@ if (!isset($_GET['code'])) {
|
|||
exit('Invalid state');
|
||||
} else {
|
||||
unset($_SESSION['provider']);
|
||||
|
||||
// Try to get an access token (using the authorization code grant)
|
||||
$token = $provider->getAccessToken(
|
||||
'authorization_code',
|
||||
array(
|
||||
[
|
||||
'code' => $_GET['code']
|
||||
)
|
||||
]
|
||||
);
|
||||
// Use this to interact with an API on the users behalf
|
||||
// Use this to get a new access token if the old one expires
|
||||
|
|
|
|||
Loading…
Reference in New Issue