Skip to content

Commit

Permalink
Changed AuthType name to 'XOAUTH2'
Browse files Browse the repository at this point in the history
Tested, fixed examples
Improved token script
Code cleanup
  • Loading branch information
Synchro committed May 21, 2015
1 parent ed41c83 commit d24a921
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
33 changes: 15 additions & 18 deletions class.oauth.php
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,23 +25,22 @@ private function getProvider() {
]);
}

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");
}


}
}
?>

2 changes: 1 addition & 1 deletion class.phpmaileroauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
6 changes: 3 additions & 3 deletions class.smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public function startTLS()
* @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)
Expand Down Expand Up @@ -389,7 +389,7 @@ public function authenticate(
);

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;
Expand Down Expand Up @@ -437,7 +437,7 @@ public function authenticate(
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)) {
Expand Down
14 changes: 6 additions & 8 deletions examples/gmail_xoauth.phps
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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";
Expand All @@ -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');

Expand All @@ -81,4 +80,3 @@ if (!$mail->send()) {
} else {
echo "Message sent!";
}
?>
13 changes: 9 additions & 4 deletions get_oauth_token.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit d24a921

Please sign in to comment.