Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Oauth2 Uncaught Error: Class 'MyOAuthTokenProvider' #2840

Closed
sfedu-admin opened this issue Dec 6, 2022 · 8 comments
Closed

Azure Oauth2 Uncaught Error: Class 'MyOAuthTokenProvider' #2840

sfedu-admin opened this issue Dec 6, 2022 · 8 comments

Comments

@sfedu-admin
Copy link

sfedu-admin commented Dec 6, 2022

Hi!
We got Token with this guide:
https://github.com/PHPMailer/PHPMailer/wiki/Microsoft-Azure-and-XOAUTH2-setup-guide
And we try to send test email with this php code example:

<?php

/**
 * This example shows how to send via Microsoft Outlook's servers using XOAUTH2 authentication
 * using the league/oauth2-client to provide the OAuth2 token.
 * To use a different OAuth2 library create a wrapper class that implements OAuthTokenProvider and
 * pass that wrapper class to PHPMailer::setOAuth().
 */


//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\OAuth;
//Alias the League Google OAuth2 provider class
use Greew\OAuth2\Client\Provider\Azure;

//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');

//Load dependencies from composer
//If this causes an error, run 'composer install'
require 'vendor/autoload.php';
//require '../vendor/autoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
//SMTP::DEBUG_OFF = off (for production use)
//SMTP::DEBUG_CLIENT = client messages
//SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;

//Set the hostname of the mail server
$mail->Host = 'smtp.office365.com';

//Set the SMTP port number:
// - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or
// - 587 for SMTP+STARTTLS
$mail->Port = 587;

//Set the encryption mechanism to use:
// - SMTPS (implicit TLS on port 465) or
// - STARTTLS (explicit TLS on port 587)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Set AuthType to use XOAUTH2
$mail->AuthType = 'XOAUTH2';

//Start Option 1: Use league/oauth2-client as OAuth2 token provider
//Fill in authentication details here
//Either the microsoft account owner, or the user that gave consent
$email = '[email protected]';
$clientId = '10a-----------';
$clientSecret = 'Apl8--------------';
$tenantId = '19ba4-----------';

//Obtained by configuring and running get_oauth_token.php
//after setting up an app in Google Developer Console.
$refreshToken = '0.AVwA-------------------';

//Create a new OAuth2 provider instance

$provider = new Azure(
    [
        'clientId' => $clientId,
        'clientSecret' => $clientSecret,
        'tenantId' => $tenantId,
    ]
);

//Pass the OAuth provider instance to PHPMailer
$mail->setOAuth(
    new OAuth(
        [
            'provider' => $provider,
            'clientId' => $clientId,
            'clientSecret' => $clientSecret,
            'refreshToken' => $refreshToken,
            'userName' => $email,
        ]
    )
);
//End Option 1

//Option 2: Another OAuth library as OAuth2 token provider
//Set up the other oauth library as per its documentation
//Then create the wrapper class that implementations OAuthTokenProvider

//$oauthTokenProvider = new MyOAuthTokenProvider(/* Email, ClientId, ClientSecret, etc. */);
$oauthTokenProvider = new MyOAuthTokenProvider(userName, clientId, clientSecret, refreshToken);



//Pass the implementation of OAuthTokenProvider to PHPMailer
$mail->setOAuth($oauthTokenProvider);
//End Option 2

//Set who the message is to be sent from
//For Outlook, this generally needs to be the same as the user you logged in as
$mail->setFrom($email, 'First Last');

//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer Outlook XOAUTH2 SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->msgHTML(file_get_contents('contentsutf8.html'), __DIR__);

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';
}

?>

We got Error:
Fatal error: Uncaught Error: Class 'MyOAuthTokenProvider' not found in /var/www/html/pmail/oauth3.php:99 Stack trace: #0 {main} thrown in /var/www/html/pmail/oauth3.php on line 99

Could you please guide us what is wrong?

@Synchro
Copy link
Member

Synchro commented Dec 6, 2022

This looks like you have either not written the code for MyOAuthTokenProvider (it is up to you to write this!), or it's not visible under that name in your current namespace.

@sfedu-admin
Copy link
Author

Sorry, I'm not php-programmer, could you please tell me, how can I verify my current namespace?
All php providers inside "vendor" folder

root@clone-pmailer:/var/www/html/pmail
# ls -l
total 104
-rw-r--r--  1 root root   191 Dec  2 11:56 composer.json
-rw-r--r--  1 root root   115 Dec  2 09:39 composer.json.bak
-rw-r--r--  1 root root 35943 Dec  2 11:26 composer.lock
-rw-r--r--  1 root root  6226 Dec  2 10:13 get_oauth_token_azure.php
-rw-r--r--  1 root root  5562 Dec  2 10:10 get_oauth_token.php
-rw-r--r--  1 root root  5667 Dec  2 10:08 get_oauth_token.php-old
-rw-r--r--  1 root root  4559 Nov  2 10:34 oauth2-1.php
-rw-r--r--  1 root root  5274 Nov  2 10:33 oauth2.php
-rw-r--r--  1 root root  5028 Dec  2 12:12 oauth3.php
-rw-r--r--  1 root root  1083 Dec  2 11:35 README.txt
drwxr-xr-x 17 root root  4096 Dec  2 11:26 vendor
-rw-r--r--  1 root root  2914 Oct 25 15:15 x2.php
root@clone-pmailer:/var/www/html/pmail
# ls -lah vendor/
total 76K
drwxr-xr-x 17 root root 4.0K Dec  2 11:26 .
drwxr-xr-x  3 root root 4.0K Dec  6 09:50 ..
-rw-r--r--  1 root root  771 Oct 25 12:43 autoload.php
drwxr-xr-x  2 root root 4.0K Dec  2 09:49 bin
drwxr-xr-x  2 root root 4.0K Dec  2 11:26 composer
drwxr-xr-x  3 root root 4.0K Oct 25 13:04 firebase
-rw-r--r--  1 root root 3.9K Oct 25 13:20 gmail.php
drwxr-xr-x  3 root root 4.0K Dec  2 10:07 greew
drwxr-xr-x  3 root root 4.0K Oct 25 13:41 greew-bak
drwxr-xr-x  5 root root 4.0K Oct 25 12:44 guzzlehttp
drwxr-xr-x  3 root root 4.0K Dec  2 11:26 league
drwxr-xr-x  3 root root 4.0K Dec  2 09:49 league-old
drwxr-xr-x  3 root root 4.0K Oct 25 12:44 paragonie
drwxr-xr-x  3 root root 4.0K Dec  2 10:07 phpmailer
drwxr-xr-x  3 root root 4.0K Oct 25 12:52 phpmailer-bak
drwxr-xr-x  5 root root 4.0K Oct 25 12:44 psr
drwxr-xr-x  3 root root 4.0K Oct 25 12:44 ralouphie
drwxr-xr-x  3 root root 4.0K Oct 25 12:44 symfony
drwxr-xr-x  3 root root 4.0K Oct 25 13:04 thenetworg

@Synchro
Copy link
Member

Synchro commented Dec 6, 2022

In the code you posted, you don't have a namespace declaration, so you are in the default (global) namespace. If you've written it yourself, your implementation of MyOAuthTokenProvider will not be in your vendor folder (that's mainly for packages written by others). You don't have to use a namespace, and I can see from your file listings that you don't have a file called MyOAuthTokenProvider.php (which is what I would expect a file containing that class to be called), so you need to write it. You might want to take a look at the Azure package that @greew wrote for how to write your own, though unless you are connecting to some obscure service, you probably want to use an existing one rather than write a new one.

@Synchro Synchro closed this as completed Dec 6, 2022
@sfedu-admin
Copy link
Author

We used all guide from:
https://github.com/PHPMailer/PHPMailer/wiki/Microsoft-Azure-and-XOAUTH2-setup-guide
But Setting up mailer with example in azure_xoauth2.phps not working for us.
Could be @greew would be so kind and give us some more details about Setting up mailer?

@Synchro
Copy link
Member

Synchro commented Dec 6, 2022

You need to define "not working" before we can help you. In the code you posted, you need to remove these two lines, which are only illustrative:

$oauthTokenProvider = new MyOAuthTokenProvider(userName, clientId, clientSecret, refreshToken);
$mail->setOAuth($oauthTokenProvider);

@greew
Copy link
Contributor

greew commented Dec 12, 2022

@sfedu-admin

As @Synchro pointed out, there are two options in the example code.

Option 1: Use a preexisting OAuth2 client
Option 2: Write your own code, that implements the necessary interfaces

If you choose to use option 1, you need to delete the Option 2 part and vice versa.

@sfedu-admin
Copy link
Author

@sfedu-admin

As @Synchro pointed out, there are two options in the example code.

Option 1: Use a preexisting OAuth2 client Option 2: Write your own code, that implements the necessary interfaces

If you choose to use option 1, you need to delete the Option 2 part and vice versa.

@greew
Much appreciation! We managed to fix this by commenting Option 2 in your example.
Thank you very much, have a nice day!

@greew
Copy link
Contributor

greew commented Dec 14, 2022

@sfedu-admin Good to hear!

And a good day to you as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants