I have been Googling for a few days trying to understand whether it is possible to use PHPMailer OAUTH2 without the need for a provider package. I have an access token management process already, so I can always arrive at the moment of sending an email, good to go with a valid, non-expired access token. All the examples of using OAUTH2 that i have found show the creation of a provider, using one of the packages, which is then fed to the mail->setOAuth() parameter. As far as I can tell, the primary function of these is to return an access token .. which I already have, along with all the other bits needed (ID, secret, etc.). I was hoping to find guidance on how to feed my access token in to the process.
I won't be surprised to learn that I am missing something plainly obvious - so if someone could point it out for me, I'd appreciate it.
The "plainly obvious" part is that all of these mechanisms work by providing an implementation of PHPMailer's OAuthTokenProvider
interface, which consists of just one method that returns a string containing the base64-encoded token, so the solution here is to make your own class that implements this interface, along the lines of:
namespace my\App;
use PHPMailer\PHPMailer\OAuthTokenProvider;
class MyTokenProvider implements OAuthTokenProvider
{
public function getOauth64()
{
//Do whatever you have to do to get the token here
//See the interface code for the expected token format
return 'my token string';
}
}
Then pass it to PHPMailer and tell it to use XOAUTH2:
$oauthTokenProvider = new \my\App\MyTokenProvider();
$mail->setOAuth($oauthTokenProvider);
$mail->AuthType = 'XOAUTH2';