Changed AuthType name to 'XOAUTH2'

Tested, fixed examples
Improved token script
Code cleanup
This commit is contained in:
Synchro 2015-05-21 03:12:17 +02:00
parent ed41c8310d
commit d24a921e52
5 changed files with 34 additions and 34 deletions

View File

@ -1,19 +1,17 @@
<?php
require_once 'vendor/autoload.php';
class OAuth {
class OAuth
{
private $oauthUserEmail = '';
private $oauthRefreshToken = '';
private $oauthClientId = '';
private $oauthClientSecret = '';
public function __construct($UserEmail,
$ClientSecret,
$ClientId,
$RefreshToken
) {
public function __construct(
$UserEmail,
$ClientSecret,
$ClientId,
$RefreshToken
) {
$this->oauthClientId = $ClientId;
$this->oauthClientSecret = $ClientSecret;
$this->oauthRefreshToken = $RefreshToken;
@ -27,23 +25,22 @@ class OAuth {
]);
}
private function getGrant(){
private function getGrant()
{
return new \League\OAuth2\Client\Grant\RefreshToken();
}
private function getToken(){
private function getToken()
{
$provider = $this->getProvider();
$grant = $this->getGrant();
return $provider->getAccessToken($grant, ['refresh_token' => $this->oauthRefreshToken]);
}
public function getOauth64(){
public function getOauth64()
{
$token = $this->getToken();
echo $this->oauthUserEmail;
return base64_encode("user=" . $this->oauthUserEmail . "\001auth=Bearer " . $token . "\001\001");
}
}
}
?>

View File

@ -21,7 +21,7 @@
* PHPMailerOAuth - PHPMailer subclass adding OAuth support.
* @package PHPMailer
* @author @sherryl4george
* @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
* @author Marcus Bointon (@Synchro) <phpmailer@synchromedia.co.uk>
*/
class PHPMailerOAuth extends PHPMailer
{

View File

@ -353,7 +353,7 @@ class SMTP
* @see hello()
* @param string $username The user name
* @param string $password The password
* @param string $authtype The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5, XOAUTH)
* @param string $authtype The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5, XOAUTH2)
* @param string $realm The auth realm for NTLM
* @param string $workstation The auth workstation for NTLM
* @param null|OAuth $OAuth An optional OAuth instance (@see PHPMailerOAuth)
@ -389,7 +389,7 @@ class SMTP
);
if (empty($authtype)) {
foreach (array('LOGIN', 'CRAM-MD5', 'NTLM', 'PLAIN') as $method) {
foreach (array('LOGIN', 'CRAM-MD5', 'NTLM', 'PLAIN', 'XOAUTH2') as $method) {
if (in_array($method, $this->server_caps['AUTH'])) {
$authtype = $method;
break;
@ -437,7 +437,7 @@ class SMTP
return false;
}
break;
case 'XOAUTH':
case 'XOAUTH2':
//If the OAuth Instance is not set. Can be a case when PHPMailer is used
//instead of PHPMailerOAuth
if (is_null($OAuth)) {

View File

@ -7,10 +7,11 @@
//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 'PHPMailerAutoload.php';
require '../PHPMailerAutoload.php';
require '../vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail = new PHPMailerOAuth;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
@ -36,8 +37,8 @@ $mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Set AuthTYpe
$mail->AuthType = 'XOAUTH';
//Set AuthType
$mail->AuthType = 'XOAUTH2';
//UserEmail to use for SMTP authentication - Use the same Email used in Google Developer Console
$mail->UserEmail = "someone@gmail.com";
@ -54,11 +55,9 @@ $mail->ClientSecret = "RANDOMCHARS-----lGyjPcRtvP";
$mail->RefreshToken = "RANDOMCHARS-----DWxgOvPT003r-yFUV49TQYag7_Aod7y0";
//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
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
@ -81,4 +80,3 @@ if (!$mail->send()) {
} else {
echo "Message sent!";
}
?>

View File

@ -12,17 +12,22 @@
* This script requires PHP 5.4 or later
*/
require './vendor/autoload.php';
require 'vendor/autoload.php';
session_start();
//If this automatic URL doesn't work, set it yourself manually
$redirectUri = isset($_SERVER['HTTPS']) ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
//$redirectUri = 'http://localhost/phpmailer/get_oauth_token.php';
$clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
$clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
//All Details Obtained by setting up APP in Google Developer Console.
//All details obtained by setting up app in Google developer console.
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
$provider = new League\OAuth2\Client\Provider\Google (
[
'clientId' => 'RANDOMCHARS----p05gduv1n2.apps.googleusercontent.com',
'clientSecret' => 'RANDOMCHARS----CWufYlGyjPcRtvP',
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $redirectUri,
'scopes' => ['https://mail.google.com/'],
'accessType' => 'offline'