diff --git a/htdocs/admin/mails.php b/htdocs/admin/mails.php
index 9145ad9a1a7..0504315075d 100644
--- a/htdocs/admin/mails.php
+++ b/htdocs/admin/mails.php
@@ -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 '
| '.$langs->trans("MAIN_MAIL_SMTPS_AUTH_TYPE").' | ';
if (empty($conf->multicompany->enabled) || ($user->admin && !$user->entity)) {
- print 'global->MAIN_MAIL_SMTPS_AUTH_TYPE == 'LOGIN' ? ' checked' : '').'> ';
+ print ' ';
print '';
print ' ';
- print 'global->MAIN_MAIL_SMTPS_AUTH_TYPE == 'XOAUTH2' ? ' checked' : '').'> ';
+ print ' ';
print '';
} 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 '';
@@ -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 ' |
| '.$langs->trans("MAIN_MAIL_SMTPS_OAUTH_SERVICE").' | ';
// SuperAdministrator access only
@@ -679,7 +682,7 @@ if ($action == 'edit') {
print ' |
';
// Host server
- if ($linuxlike && (isset($conf->global->MAIN_MAIL_SENDMODE) && $conf->global->MAIN_MAIL_SENDMODE == 'mail')) {
+ if ($linuxlike && (getDolGlobalString('MAIN_MAIL_SENDMODE') == 'mail')) {
print '| '.$langs->trans("MAIN_MAIL_SMTP_SERVER_NotAvailableOnLinuxLike").' | '.$langs->trans("SeeLocalSendMailSetup").' |
';
} else {
print '| '.$langs->trans("MAIN_MAIL_SMTP_SERVER", ini_get('SMTP') ?ini_get('SMTP') : $langs->transnoentities("Undefined")).' | '.(!empty($conf->global->MAIN_MAIL_SMTP_SERVER) ? $conf->global->MAIN_MAIL_SMTP_SERVER : '').' |
';
@@ -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 '| '.$langs->trans("MAIN_MAIL_SMTP_PORT_NotAvailableOnLinuxLike").' | '.$langs->trans("SeeLocalSendMailSetup").' |
';
} else {
print '| '.$langs->trans("MAIN_MAIL_SMTP_PORT", ini_get('smtp_port') ?ini_get('smtp_port') : $langs->transnoentities("Undefined")).' | '.(!empty($conf->global->MAIN_MAIL_SMTP_PORT) ? $conf->global->MAIN_MAIL_SMTP_PORT : '').' |
';
}
// 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 '| '.$langs->trans("MAIN_MAIL_SMTPS_ID").' | '.$conf->global->MAIN_MAIL_SMTPS_ID.' |
';
}
// 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 '| '.$langs->trans("MAIN_MAIL_SMTPS_AUTH_TYPE").' | '.$text.' |
';
}
// 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 '| '.$langs->trans("MAIN_MAIL_SMTPS_PW").' | '.preg_replace('/./', '*', $conf->global->MAIN_MAIL_SMTPS_PW).' |
';
}
// 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();
diff --git a/htdocs/core/actions_sendmails.inc.php b/htdocs/core/actions_sendmails.inc.php
index 7494f5a3668..3749b403469 100644
--- a/htdocs/core/actions_sendmails.inc.php
+++ b/htdocs/core/actions_sendmails.inc.php
@@ -427,9 +427,14 @@ if (($action == 'send' || $action == 'relance') && !GETPOST('addfile') && !GETPO
} else {
$langs->load("other");
$mesg = '';
- if ($mailfile->error) {
+ if (!empty($mailfile->error) || !empty($mailfile->errors)) {
$mesg .= $langs->transnoentities('ErrorFailedToSendMail', dol_escape_htmltag($from), dol_escape_htmltag($sendto));
- $mesg .= '
'.$mailfile->error;
+ if (!empty($mailfile->error)) {
+ $mesg .= '
'.$mailfile->error;
+ }
+ if (!empty($mailfile->errors) && is_array($mailfile->errors)) {
+ $mesg .= '
'.implode('
', $mailfile->errors);
+ }
} else {
$mesg .= $langs->transnoentities('ErrorFailedToSendMail', dol_escape_htmltag($from), dol_escape_htmltag($sendto));
if (!empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php
index 1b03ed0d604..54573f92c7a 100644
--- a/htdocs/core/class/CMailFile.class.php
+++ b/htdocs/core/class/CMailFile.class.php
@@ -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;
diff --git a/htdocs/langs/fr_FR/admin.lang b/htdocs/langs/fr_FR/admin.lang
index 42d973e8e06..18c2dfc615d 100644
--- a/htdocs/langs/fr_FR/admin.lang
+++ b/htdocs/langs/fr_FR/admin.lang
@@ -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
\ No newline at end of file
+UsePassword=Utiliser un mot de passe
+UseOauth=Utiliser un jeton d'authentification OAUTH