Skip to content

Commit

Permalink
Internal: Mail: Add options to allow configuring mail with XOAuth met…
Browse files Browse the repository at this point in the history
…hod - refs BT#20512

Author: @AngelFQC
  • Loading branch information
AngelFQC committed Feb 14, 2023
1 parent 166343e commit 3748da2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/config/mail.conf.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@
// showing it as a loose JSON string to the final user. If this is your case,
// you might want to set the variable below to 'false' to disable this header.
$platform_email['EXCLUDE_JSON'] = false;

// Fill the following only for mail services with OAuth2.0 authentication. Otherwise leave untouched.
$platform_email['XOAUTH2_METHOD'] = false;
$platform_email['XOAUTH2_URL_AUTHORIZE'] = 'https://provider.example/oauth2/auth';
$platform_email['XOAUTH2_URL_ACCES_TOKEN'] = 'https://provider.example/token';
$platform_email['XOAUTH2_URL_RESOURCE_OWNER_DETAILS'] = 'https://provider.example/userinfo';
$platform_email['XOAUTH2_SCOPES'] = '';
$platform_email['XOAUTH2_CLIENT_ID'] = '';
$platform_email['XOAUTH2_CLIENT_SECRET'] = '';
$platform_email['XOAUTH2_REFRESH_TOKEN'] = '';
24 changes: 24 additions & 0 deletions main/inc/lib/api.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Chamilo\CourseBundle\Entity\CItemProperty;
use Chamilo\UserBundle\Entity\User;
use ChamiloSession as Session;
use League\OAuth2\Client\Provider\GenericProvider;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\PHPMailer;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -9426,6 +9428,28 @@ function api_mail_html(
}

$mail = new PHPMailer();

if (!empty($platform_email['XOAUTH2_METHOD'])) {
$provider = new GenericProvider([
'clientId' => $platform_email['XOAUTH2_CLIENT_ID'],
'clientSecret' => $platform_email['XOAUTH2_CLIENT_SECRET'],
'urlAuthorize' => $platform_email['XOAUTH2_URL_AUTHORIZE'],
'urlAccessToken' => $platform_email['XOAUTH2_URL_ACCES_TOKEN'],
'urlResourceOwnerDetails' => $platform_email['XOAUTH2_URL_RESOURCE_OWNER_DETAILS'],
'scopes' => $platform_email['XOAUTH2_SCOPES'],
]);
$mail->AuthType = 'XOAUTH2';
$mail->setOAuth(
new OAuth([
'provider' => $provider,
'clientId' => $platform_email['XOAUTH2_CLIENT_ID'],
'clientSecret' => $platform_email['XOAUTH2_CLIENT_SECRET'],
'refreshToken' => $platform_email['XOAUTH2_REFRESH_TOKEN'],
'userName' => $platform_email['SMTP_USER'],
])
);
}

$mail->Mailer = $platform_email['SMTP_MAILER'];
$mail->Host = $platform_email['SMTP_HOST'];
$mail->Port = $platform_email['SMTP_PORT'];
Expand Down

0 comments on commit 3748da2

Please sign in to comment.