mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 10:26:40 +02:00
MDL-61921 admin: Support XOAUTH2 for outgoing mail
This commit is contained in:
parent
44ca802d4e
commit
bc80531188
6 changed files with 121 additions and 3 deletions
|
@ -54,6 +54,10 @@ class moodle_phpmailer extends \PHPMailer\PHPMailer\PHPMailer {
|
|||
|
||||
if (!empty($CFG->smtpauthtype)) {
|
||||
$this->AuthType = $CFG->smtpauthtype;
|
||||
|
||||
if ($this->AuthType == 'XOAUTH2') {
|
||||
$this->process_oauth();
|
||||
}
|
||||
}
|
||||
|
||||
// Some MTAs may do double conversion of LF if CRLF used, CRLF is required line ending in RFC 822bis.
|
||||
|
@ -65,7 +69,7 @@ class moodle_phpmailer extends \PHPMailer\PHPMailer\PHPMailer {
|
|||
}
|
||||
|
||||
/**
|
||||
* Extended AddCustomHeader function in order to stop duplicate
|
||||
* Extended AddCustomHeader function in order to stop duplicate
|
||||
* message-ids
|
||||
* http://tracker.moodle.org/browse/MDL-3681
|
||||
*/
|
||||
|
@ -83,7 +87,7 @@ class moodle_phpmailer extends \PHPMailer\PHPMailer\PHPMailer {
|
|||
|
||||
/**
|
||||
* Use internal moodles own core_text to encode mimeheaders.
|
||||
* Fall back to phpmailers inbuilt functions if not
|
||||
* Fall back to phpmailers inbuilt functions if not
|
||||
*/
|
||||
public function encodeHeader($str, $position = 'text') {
|
||||
$encoded = core_text::encode_mimeheader($str, $this->CharSet);
|
||||
|
@ -142,4 +146,32 @@ class moodle_phpmailer extends \PHPMailer\PHPMailer\PHPMailer {
|
|||
return parent::postSend();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Config the PHPMailer to use OAUTH if necessary.
|
||||
*/
|
||||
private function process_oauth(): void {
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->libdir . '/phpmailer/moodle_phpmailer_oauth.php');
|
||||
if (!empty($CFG->smtpoauthservice)) {
|
||||
// Get the issuer.
|
||||
$issuer = \core\oauth2\api::get_issuer($CFG->smtpoauthservice);
|
||||
// Validate the issuer and check if it is enabled or not.
|
||||
if ($issuer && $issuer->get('enabled')) {
|
||||
// Get the OAuth Client.
|
||||
if ($oauthclient = \core\oauth2\api::get_system_oauth_client($issuer)) {
|
||||
$oauth = new moodle_phpmailer_oauth([
|
||||
'provider' => $oauthclient,
|
||||
'clientId' => $oauthclient->get_clientid(),
|
||||
'clientSecret' => $oauthclient->get_clientsecret(),
|
||||
'refreshToken' => $oauthclient->get_refresh_token(),
|
||||
'userName' => $CFG->smtpuser,
|
||||
]);
|
||||
// Set the OAuth.
|
||||
$this->setOAuth($oauth);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
30
lib/phpmailer/moodle_phpmailer_oauth.php
Normal file
30
lib/phpmailer/moodle_phpmailer_oauth.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Moodle Customised version of the PHPMailer OAuth class
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2022 Huong Nguyen <huongnv13@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class moodle_phpmailer_oauth extends \PHPMailer\PHPMailer\OAuth {
|
||||
|
||||
protected function getToken() {
|
||||
return $this->provider->get_accesstoken()->token;
|
||||
}
|
||||
|
||||
}
|
|
@ -7,7 +7,7 @@ For more information on this version of PHPMailer, check out https://github.com/
|
|||
|
||||
To upgrade this library:
|
||||
1. Download the latest release of PHPMailer in https://github.com/PHPMailer/PHPMailer/releases.
|
||||
2. Remove everything inside lib/phpmailer/ folder except README_MOODLE.txt and moodle_phpmailer.php.
|
||||
2. Remove everything inside lib/phpmailer/ folder except README_MOODLE.txt, moodle_phpmailer.php and moodle_phpmailer_oauth.php.
|
||||
3. Extract the contents of the release archive to lib/phpmailer.
|
||||
4. Remove the following files that were extracted:
|
||||
- composer.json
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue