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

Already having the token, send mail using ms office oauth2 authentication #2813

Closed
nettunodev opened this issue Oct 26, 2022 · 3 comments
Closed

Comments

@nettunodev
Copy link

nettunodev commented Oct 26, 2022

Hi there, is there already a way to authenticate using xoauth2 authentication without the need to get a token?
I already have the token and to get PHPMailer to use it I had to:

  • set the AuthType parameter to XOAUTH2;
    $mail->oauth = $oauthToken;

  • modify the SMTP.php implementation (line 598) like this:

    // $oauth = $OAuth->getOauth64();

    // Adding new lines, username and token base64 encoding
    $oauth = base64_encode(
                  'user=' .
                 $username .
                  "\001auth=Bearer " .
                  $OAuth .
                  "\001\001");
    
  • modify the PHPMailer.php implementation making the $oauth variable (line 365) public
    public $oauth;

  • set the now available oauth parameter, like this
    $mail->oauth = $oauthToken;

This way I was able to authenticate to the MS server and send an email.
Maybe there's already another way to do it but I didn't find it.

@decomplexity
Copy link
Contributor

One problem with driving PHPMailer with an externally-generated access token is that the token expires very quickly. Using a refresh token (which MSFT expires after 90 days for most clients) is far more manageable.

@nettunodev
Copy link
Author

One problem with driving PHPMailer with an externally-generated access token is that the token expires very quickly. Using a refresh token (which MSFT expires after 90 days for most clients) is far more manageable.

I already have a function that I call before using the token and that check the validity and eventually refresh it. I don't need PHPMailer to do it for me. Maybe that's also the case for other users...

@greew
Copy link
Contributor

greew commented Nov 20, 2022

Well... you could of course create your own class, that implements PHPMailer\PHPMailer\OAuthTokenProvider and use that instead of an actual oauth library.

That ought to give you, what you need?

Something like

<?php

class Provider implements \PHPMailer\PHPMailer\OAuthTokenProvider {
    private string $username;
    private string $oauth;

    public function __construct($username, $oauth) {
        $this->username = $username;
        $this->oauth = $oauth;
    }

    public function getOauth64() {
        return base64_encode(
              'user=' .
             $this->username .
              "\001auth=Bearer " .
              $this->oauth .
              "\001\001");
    }
}

And then use that in your mailer

<?php

$provider = new Provider('username', 'token');

$mailer = new PHPMailer();
...
$mailer->setOAuth($provider);
$mailer->send();

@Synchro Synchro closed this as completed Apr 10, 2024
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

4 participants