Internal: Mail: Add options to allow configuring mail with XOAuth method - refs BT#20512

Author: @AngelFQC
pull/4614/head
Angel Fernando Quiroz Campos 3 years ago committed by GitHub
parent 166343ec48
commit 3748da213f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      app/config/mail.conf.dist.php
  2. 24
      main/inc/lib/api.lib.php

@ -41,3 +41,13 @@ $platform_email['DKIM_PRIVATE_KEY'] = ''; //the private key as the path to a fil
// 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'] = '';

@ -6,6 +6,8 @@ use Chamilo\CoreBundle\Entity\SettingsCurrent;
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;
@ -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'];

Loading…
Cancel
Save