Merge pull request #21700 from frederic34/outh2forswiftmailer

add oauth2 for swiftmailer
This commit is contained in:
Laurent Destailleur 2022-08-11 01:27:53 +02:00 committed by GitHub
commit abfaefbe41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 22 deletions

View File

@ -292,7 +292,7 @@ if ($action == 'edit') {
jQuery("#smtp_port_mess").hide();
jQuery(".smtp_method").show();
jQuery(".dkim").show();
jQuery(".smtp_auth_method").hide();
jQuery(".smtp_auth_method").show();
}
}
function change_smtp_auth_method() {
@ -300,6 +300,9 @@ if ($action == 'edit') {
if (jQuery("#MAIN_MAIL_SENDMODE").val()==\'smtps\' && jQuery("#radio_oauth").prop("checked")) {
jQuery(".smtp_oauth_service").show();
jQuery(".smtp_pw").hide();
} else if (jQuery("#MAIN_MAIL_SENDMODE").val()==\'swiftmailer\' && jQuery("#radio_oauth").prop("checked")) {
jQuery(".smtp_oauth_service").show();
jQuery(".smtp_pw").hide();
} else if(jQuery("#MAIN_MAIL_SENDMODE").val()==\'mail\'){
jQuery(".smtp_oauth_service").hide();
jQuery(".smtp_pw").hide();
@ -465,13 +468,13 @@ if ($action == 'edit') {
if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))) {
print '<tr class="oddeven smtp_auth_method"><td>'.$langs->trans("MAIN_MAIL_SMTPS_AUTH_TYPE").'</td><td>';
if (empty($conf->multicompany->enabled) || ($user->admin && !$user->entity)) {
print '<input type="radio" id="radio_pw" name="MAIN_MAIL_SMTPS_AUTH_TYPE" value="LOGIN"'.($conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE == 'LOGIN' ? ' checked' : '').'> ';
print '<input type="radio" id="radio_pw" name="MAIN_MAIL_SMTPS_AUTH_TYPE" value="LOGIN"'.(getDolGlobalString('MAIN_MAIL_SMTPS_AUTH_TYPE') == 'LOGIN' ? ' checked' : '').'> ';
print '<label for="radio_pw" >'.$langs->trans("UsePassword").'</label>';
print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
print '<input type="radio" id="radio_oauth" name="MAIN_MAIL_SMTPS_AUTH_TYPE" value="XOAUTH2"'.($conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE == 'XOAUTH2' ? ' checked' : '').'> ';
print '<input type="radio" id="radio_oauth" name="MAIN_MAIL_SMTPS_AUTH_TYPE" value="XOAUTH2"'.(getDolGlobalString('MAIN_MAIL_SMTPS_AUTH_TYPE') == 'XOAUTH2' ? ' checked' : '').'> ';
print '<label for="radio_oauth" >'.$form->textwithpicto($langs->trans("UseOauth"), $langs->trans("OauthNotAvailableForAllAndHadToBeCreatedBefore")).'</label>';
} else {
$value = $conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE;
$value = getDolGlobalString('MAIN_MAIL_SMTPS_AUTH_TYPE', 'LOGIN');
$htmltext = $langs->trans("ContactSuperAdminForChange");
print $form->textwithpicto($langs->trans("MAIN_MAIL_SMTPS_AUTH_TYPE"), $htmltext, 1, 'superadmin');
print '<input type="hidden" id="MAIN_MAIL_SMTPS_AUTH_TYPE" name="MAIN_MAIL_SMTPS_AUTH_TYPE" value="'.$value.'">';
@ -497,7 +500,7 @@ if ($action == 'edit') {
}
// OAUTH service provider
if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps')))) {
if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))) {
print '<tr class="oddeven smtp_oauth_service"><td>'.$langs->trans("MAIN_MAIL_SMTPS_OAUTH_SERVICE").'</td><td>';
// SuperAdministrator access only
@ -679,7 +682,7 @@ if ($action == 'edit') {
print '</td></tr>';
// Host server
if ($linuxlike && (isset($conf->global->MAIN_MAIL_SENDMODE) && $conf->global->MAIN_MAIL_SENDMODE == 'mail')) {
if ($linuxlike && (getDolGlobalString('MAIN_MAIL_SENDMODE') == 'mail')) {
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTP_SERVER_NotAvailableOnLinuxLike").'</td><td><span class="opacitymedium">'.$langs->trans("SeeLocalSendMailSetup").'</span></td></tr>';
} else {
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTP_SERVER", ini_get('SMTP') ?ini_get('SMTP') : $langs->transnoentities("Undefined")).'</td><td>'.(!empty($conf->global->MAIN_MAIL_SMTP_SERVER) ? $conf->global->MAIN_MAIL_SMTP_SERVER : '').'</td></tr>';
@ -687,31 +690,31 @@ if ($action == 'edit') {
// Port
if ($linuxlike && (isset($conf->global->MAIN_MAIL_SENDMODE) && $conf->global->MAIN_MAIL_SENDMODE == 'mail')) {
if ($linuxlike && (getDolGlobalString('MAIN_MAIL_SENDMODE') == 'mail')) {
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTP_PORT_NotAvailableOnLinuxLike").'</td><td><span class="opacitymedium">'.$langs->trans("SeeLocalSendMailSetup").'</span></td></tr>';
} else {
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTP_PORT", ini_get('smtp_port') ?ini_get('smtp_port') : $langs->transnoentities("Undefined")).'</td><td>'.(!empty($conf->global->MAIN_MAIL_SMTP_PORT) ? $conf->global->MAIN_MAIL_SMTP_PORT : '').'</td></tr>';
}
// SMTPS ID
if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer'))) {
if (in_array(getDolGlobalString('MAIN_MAIL_SENDMODE'), array('smtps', 'swiftmailer'))) {
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTPS_ID").'</td><td>'.$conf->global->MAIN_MAIL_SMTPS_ID.'</td></tr>';
}
// AUTH method
if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps'))) {
$authtype = $conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE;
if (in_array(getDolGlobalString('MAIN_MAIL_SENDMODE'), array('smtps', 'swiftmailer'))) {
$authtype = getDolGlobalString('MAIN_MAIL_SMTPS_AUTH_TYPE', 'LOGIN');
$text = ($authtype === "LOGIN") ? $langs->trans("UsePassword") : ($authtype === "XOAUTH2" ? $langs->trans("UseOauth") : '') ;
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTPS_AUTH_TYPE").'</td><td>'.$text.'</td></tr>';
}
// SMTPS PW
if (isset($conf->global->MAIN_MAIL_SENDMODE) && (in_array($conf->global->MAIN_MAIL_SENDMODE, array('swiftmailer')) || (in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps')) && (isset($conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE) && $conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE === "LOGIN")))) {
if (in_array(getDolGlobalString('MAIN_MAIL_SENDMODE'), array('smtps', 'swiftmailer')) && getDolGlobalString('MAIN_MAIL_SMTPS_AUTH_TYPE') != "XOAUTH2") {
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTPS_PW").'</td><td>'.preg_replace('/./', '*', $conf->global->MAIN_MAIL_SMTPS_PW).'</td></tr>';
}
// SMTPS oauth service
if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps')) && isset($conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE) && $conf->global->MAIN_MAIL_SMTPS_AUTH_TYPE === "XOAUTH2") {
if (in_array(getDolGlobalString('MAIN_MAIL_SENDMODE'), array('smtps', 'swiftmailer')) && getDolGlobalString('MAIN_MAIL_SMTPS_AUTH_TYPE') === "XOAUTH2") {
$text = $oauthservices[$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE];
if (empty($text)) {
$text = $langs->trans("Undefined").img_warning();

View File

@ -427,9 +427,14 @@ if (($action == 'send' || $action == 'relance') && !GETPOST('addfile') && !GETPO
} else {
$langs->load("other");
$mesg = '<div class="error">';
if ($mailfile->error) {
if (!empty($mailfile->error) || !empty($mailfile->errors)) {
$mesg .= $langs->transnoentities('ErrorFailedToSendMail', dol_escape_htmltag($from), dol_escape_htmltag($sendto));
$mesg .= '<br>'.$mailfile->error;
if (!empty($mailfile->error)) {
$mesg .= '<br>'.$mailfile->error;
}
if (!empty($mailfile->errors) && is_array($mailfile->errors)) {
$mesg .= '<br>'.implode('<br>', $mailfile->errors);
}
} else {
$mesg .= $langs->transnoentities('ErrorFailedToSendMail', dol_escape_htmltag($from), dol_escape_htmltag($sendto));
if (!empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {

View File

@ -72,9 +72,29 @@ class CMailFile
*/
public $error = '';
/**
* @var string[] Array of Error code (or message)
*/
public $errors = array();
public $smtps; // Contains SMTPs object (if this method is used)
public $phpmailer; // Contains PHPMailer object (if this method is used)
/**
* @var Swift_SmtpTransport
*/
public $transport;
/**
* @var Swift_Mailer
*/
public $mailer;
/**
* @var Swift_Plugins_Loggers_ArrayLogger
*/
public $logger;
/**
* @var string CSS
*/
@ -847,7 +867,7 @@ class CMailFile
$this->smtps->setPW($loginpass);
}
if (!empty($conf->global->$keyforsmtpauthtype) && $conf->global->$keyforsmtpauthtype === "XOAUTH2") {
if (getDolGlobalString($keyforsmtpauthtype) === "XOAUTH2") {
require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array
$keyforsupportedoauth2array = $conf->global->$keyforsmtpoauthservice;
if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
@ -897,7 +917,6 @@ class CMailFile
}
$result = $this->smtps->sendMsg();
//print $result;
if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
$this->dump_mail();
@ -943,9 +962,39 @@ class CMailFile
if (!empty($conf->global->$keyforsmtpid)) {
$this->transport->setUsername($conf->global->$keyforsmtpid);
}
if (!empty($conf->global->$keyforsmtppw)) {
if (!empty($conf->global->$keyforsmtppw) && getDolGlobalString($keyforsmtpauthtype) != "XOAUTH2") {
$this->transport->setPassword($conf->global->$keyforsmtppw);
}
if (getDolGlobalString($keyforsmtpauthtype) === "XOAUTH2") {
require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array
$keyforsupportedoauth2array = getDolGlobalString($keyforsmtpoauthservice);
if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
$keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
} else {
$keyforprovider = '';
}
$keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
$keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
$OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['name'].($keyforprovider ? '-'.$keyforprovider : ''));
require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
$storage = new DoliStorage($db, $conf);
try {
$tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
if (is_object($tokenobj)) {
$this->transport->setAuthMode('XOAUTH2');
$this->transport->setPassword($tokenobj->getAccessToken());
} else {
$this->errors[] = "Token not found";
}
} catch (Exception $e) {
// Return an error if token not found
$this->errors[] = $e->getMessage();
dol_syslog("CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
}
}
if (!empty($conf->global->$keyforsslseflsigned)) {
$this->transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
}
@ -978,16 +1027,16 @@ class CMailFile
try {
$result = $this->mailer->send($this->message, $failedRecipients);
} catch (Exception $e) {
$this->error = $e->getMessage();
$this->errors[] = $e->getMessage();
}
if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
$this->dump_mail();
}
$res = true;
if (!empty($this->error) || !$result) {
if (!empty($this->error) || !empty($this->errors) || !$result) {
if (!empty($failedRecipients)) {
$this->error = 'Transport failed for the following addresses: "' . join('", "', $failedRecipients) . '".';
$this->errors[] = 'Transport failed for the following addresses: "' . join('", "', $failedRecipients) . '".';
}
dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
$res = false;

View File

@ -2291,5 +2291,5 @@ OauthNotAvailableForAllAndHadToBeCreatedBefore=L'authentification OAUTH2 n'est p
MAIN_MAIL_SMTPS_OAUTH_SERVICE=Service d'authentification OAUTH2
DontForgetCreateTokenOauthMod=Un jeton avec les bonnes permissions doit avoir été créé en amont avec le module OAUTH
MAIN_MAIL_SMTPS_AUTH_TYPE=Méthode d'authentication
UsePassword=Utiliser un mot de pass
UseOauth=Utiliser un jeton d'authentification OAUTH
UsePassword=Utiliser un mot de passe
UseOauth=Utiliser un jeton d'authentification OAUTH