From 739127f8e9ac4c27f5349b9a42176b3eef476baf Mon Sep 17 00:00:00 2001 From: Marcus Date: Tue, 5 Apr 2016 13:09:53 +0200 Subject: [PATCH] Code cleanup, see #675 --- examples/gmail_xoauth.phps | 46 +++++++++++++------------------------- get_oauth_token.php | 16 +++++-------- 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/examples/gmail_xoauth.phps b/examples/gmail_xoauth.phps index 2e2bc11e..409863b5 100644 --- a/examples/gmail_xoauth.phps +++ b/examples/gmail_xoauth.phps @@ -1,23 +1,17 @@ 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; diff --git a/get_oauth_token.php b/get_oauth_token.php index 744be0f7..fa193382 100644 --- a/get_oauth_token.php +++ b/get_oauth_token.php @@ -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