';
if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer'))) {
diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php
index 94fb05d5346..24c27b47795 100644
--- a/htdocs/core/class/CMailFile.class.php
+++ b/htdocs/core/class/CMailFile.class.php
@@ -30,6 +30,7 @@
* \brief File of class to send emails (with attachments or not)
*/
+use OAuth\Common\Storage\DoliStorage;
/**
* Class to send emails (with attachments or not)
* Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto);
@@ -657,6 +658,8 @@ class CMailFile
}
$keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER';
+ $keyforsmtpuseoauth = "MAIN_MAIL_SMTPS_USE_OAUTH";
+ $keyforsmtpoauthservice = "MAIN_MAIL_SMTPS_OAUTH_SERVICE";
$keyforsmtpport = 'MAIN_MAIL_SMTP_PORT';
$keyforsmtpid = 'MAIN_MAIL_SMTPS_ID';
$keyforsmtppw = 'MAIN_MAIL_SMTPS_PW';
@@ -832,6 +835,36 @@ class CMailFile
$this->smtps->setPW($loginpass);
}
+ if (!empty($conf->global->$keyforsmtpuseoauth)) {
+ require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array
+ $keyforsupportedoauth2array = $conf->global->$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->smtps->setToken($tokenobj->getAccessToken());
+ } else {
+ $this->error = "Token not found";
+ }
+ } catch (Exception $e) {
+ // Return an error if token not found
+ $this->error = $e->getMessage();
+ dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
+ }
+ }
+
$res = true;
$from = $this->smtps->getFrom('org');
if ($res && !$from) {
diff --git a/htdocs/core/class/smtps.class.php b/htdocs/core/class/smtps.class.php
index 1f5a48a517d..05248e89143 100644
--- a/htdocs/core/class/smtps.class.php
+++ b/htdocs/core/class/smtps.class.php
@@ -68,6 +68,11 @@ class SMTPs
*/
private $_smtpsPW = null;
+ /**
+ * Token in case we use OAUTH2
+ */
+ private $_smtpsToken = null;
+
/**
* Who sent the Message
* This can be defined via a INI file or via a setter method
@@ -583,9 +588,10 @@ class SMTPs
break;
case 'XOAUTH2':
// "user=$email\1auth=Bearer $token\1\1"
- $token = 'xxx';
- $xxxx = "user=".$this->_smtpsID."\1auth=Bearer ".$token."\1\1";
- $_retVal = $this->socket_send_str('AUTH XOAUTH2 '.base64_encode($xxxx), '235');
+ $user = $this->_smtpsID;
+ $token = $this->_smtpsToken;
+ $initRes = "user=".$user."\001auth=Bearer ".$token."\001\001";
+ $_retVal = $this->socket_send_str('AUTH XOAUTH2 '.base64_encode($initRes), '235');
if (!$_retVal) {
$this->_setErr(130, 'Error when asking for AUTH XOAUTH2');
}
@@ -631,7 +637,7 @@ class SMTPs
// Connect to Server
if ($this->socket = $this->_server_connect()) {
// If a User ID *and* a password is given, assume Authentication is desired
- if (!empty($this->_smtpsID) && !empty($this->_smtpsPW)) {
+ if (!empty($this->_smtpsID) && (!empty($this->_smtpsPW) || !empty($this->_smtpsToken))) {
// Send the RFC2554 specified EHLO.
$_retVal = $this->_server_authenticate();
} else {
@@ -923,6 +929,27 @@ class SMTPs
return $this->_smtpsPW;
}
+ /**
+ * User token for OAUTH2
+ *
+ * @param string $_strToken User token
+ * @return void
+ */
+ public function setToken($_strToken)
+ {
+ $this->_smtpsToken = $_strToken;
+ }
+
+ /**
+ * Retrieves the User token for OAUTH2
+ *
+ * @return string User token for OAUTH2
+ */
+ public function getToken()
+ {
+ return $this->_smtpsToken;
+ }
+
/**
* Character set used for current message
* Character set is defaulted to 'iso-8859-1';
@@ -1866,7 +1893,7 @@ class SMTPs
}
if (!(substr($server_response, 0, 3) == $response)) {
- $this->_setErr(120, "Ran into problems sending Mail.\r\nResponse: $server_response");
+ $this->_setErr(120, "Ran into problems sending Mail.\r\nResponse:".$server_response);
$_retVal = false;
}
diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang
index c1f01ae85d9..e5b8fe597bc 100644
--- a/htdocs/langs/en_US/admin.lang
+++ b/htdocs/langs/en_US/admin.lang
@@ -2286,4 +2286,8 @@ DoesNotWorkWithAllThemes=Will not work with all themes
NoName=No name
ShowAdvancedOptions= Show advanced options
HideAdvancedoptions= Hide advanced options
-CIDLookupURL=The module brings an URL that can be used by an external tool to get the name of a thirdparty or contact from its phone number. URL to use is:
+CIDLookupURL=The module brings an URL that can be used by an external tool to get the name of a thirdparty or contact from its phone number. URL to use is:
+MAIN_MAIL_SMTPS_USE_OAUTH=Use OAUTH2 authentication
+OauthNotAvailableForAll=OAUTH2 authentication is not available for all hosts
+MAIN_MAIL_SMTPS_OAUTH_SERVICE=OAUTH2 authentication service
+DontForgetCreateTokenOauthMod=A token with the right permissions must have been created upstream with the OAUTH module
\ No newline at end of file
diff --git a/htdocs/langs/fr_FR/admin.lang b/htdocs/langs/fr_FR/admin.lang
index f374349d72f..6302377b449 100644
--- a/htdocs/langs/fr_FR/admin.lang
+++ b/htdocs/langs/fr_FR/admin.lang
@@ -2286,3 +2286,8 @@ NoName=Sans nom
ShowAdvancedOptions= Show advanced options
HideAdvancedoptions= Hide advanced options
CIDLookupURL=The module brings an URL that can be used by an external tool to get the name of a thirdparty or contact from its phone number. URL to use is:
+DoesNotWorkWithAllThemes=Ne fonctionne pas avec tous les thèmes
+MAIN_MAIL_SMTPS_USE_OAUTH=Utilisation de l'authentification OAUTH2
+OauthNotAvailableForAll=L'authentification OAUTH2 n'est pas disponible pour tous les hôtes
+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