NEW: MAIN_MAIL_AUTOCOPY_TO can accept several email and special keys

This commit is contained in:
Laurent Destailleur 2022-03-17 12:39:39 +01:00
parent 43e7547611
commit 5709f168a8
3 changed files with 47 additions and 11 deletions

View File

@ -46,11 +46,12 @@ if ($action == 'test' || $action == 'send') {
$substitutionarrayfortest = array(
'__DOL_MAIN_URL_ROOT__'=>DOL_MAIN_URL_ROOT,
'__CHECK_READ__' => (!empty($object) && is_object($object) && is_object($object->thirdparty)) ? '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.$object->thirdparty->tag.'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '',
'__USER_LOGIN__' => $user->login,
'__USER_EMAIL__' => $user->email,
'__USER_SIGNATURE__' => (($user->signature && empty($conf->global->MAIN_MAIL_DO_NOT_USE_SIGN)) ? $usersignature : ''), // Done into actions_sendmails
'__ID__' => 'RecipientIdRecord',
//'__EMAIL__' => 'RecipientEMail', // Done into actions_sendmails
'__CHECK_READ__' => (!empty($object) && is_object($object) && is_object($object->thirdparty)) ? '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.$object->thirdparty->tag.'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '',
'__USER_SIGNATURE__' => (($user->signature && empty($conf->global->MAIN_MAIL_DO_NOT_USE_SIGN)) ? $usersignature : ''), // Done into actions_sendmails
'__LOGIN__' => $user->login,
'__LASTNAME__' => 'RecipientLastname',
'__FIRSTNAME__' => 'RecipientFirstname',
'__ADDRESS__'=> 'RecipientAddress',
@ -762,9 +763,18 @@ if ($action == 'edit') {
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_AUTOCOPY_TO").'</td>';
print '<td>';
if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) {
print $conf->global->MAIN_MAIL_AUTOCOPY_TO;
if (!isValidEmail($conf->global->MAIN_MAIL_AUTOCOPY_TO)) {
print img_warning($langs->trans("ErrorBadEMail"));
$listofemail = explode(',', $conf->global->MAIN_MAIL_AUTOCOPY_TO);
$i = 0;
foreach ($listofemail as $key => $val) {
if ($i) {
print ', ';
}
$val = trim($val);
print $val;
if (!isValidEmail($val, 0, 1)) {
print img_warning($langs->trans("ErrorBadEMail", $val));
}
$i++;
}
} else {
print '&nbsp;';

View File

@ -140,7 +140,7 @@ class CMailFile
*/
public function __construct($subject, $to, $from, $msg, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array(), $addr_cc = "", $addr_bcc = "", $deliveryreceipt = 0, $msgishtml = 0, $errors_to = '', $css = '', $trackid = '', $moreinheader = '', $sendcontext = 'standard', $replyto = '')
{
global $conf, $dolibarr_main_data_root;
global $conf, $dolibarr_main_data_root, $user;
// Clean values of $mimefilename_list
if (is_array($mimefilename_list)) {
@ -251,9 +251,31 @@ class CMailFile
}
}
// Add autocopy to if not already in $to (Note: Adding bcc for specific modules are also done from pages)
if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO) && !preg_match('/'.preg_quote($conf->global->MAIN_MAIL_AUTOCOPY_TO, '/').'/i', $to)) {
$addr_bcc .= ($addr_bcc ? ', ' : '').$conf->global->MAIN_MAIL_AUTOCOPY_TO;
// Add auto copy to if not already in $to (Note: Adding bcc for specific modules are also done from pages)
// For example MAIN_MAIL_AUTOCOPY_TO can be 'email@example.com, __USER_EMAIL__, ...'
if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) {
$listofemailstoadd = explode(',', $conf->global->MAIN_MAIL_AUTOCOPY_TO);
foreach ($listofemailstoadd as $key => $val) {
$emailtoadd = $listofemailstoadd[$key];
if (trim($emailtoadd) == '__USER_EMAIL__') {
if (!empty($user) && !empty($user->email)) {
$emailtoadd = $user->email;
} else {
$emailtoadd = '';
}
}
if ($emailtoadd && preg_match('/'.preg_quote($emailtoadd, '/').'/i', $to)) {
$emailtoadd = ''; // Email already in the "To"
}
if ($emailtoadd) {
$listofemailstoadd[$key] = $emailtoadd;
} else {
unset($listofemailstoadd[$key]);
}
}
if (!empty($listofemailstoadd)) {
$addr_bcc .= ($addr_bcc ? ', ' : '').join(', ', $listofemailstoadd);
}
}
$this->subject = $subject;

View File

@ -3465,14 +3465,18 @@ function dol_print_address($address, $htmlid, $element, $id, $noprint = 0, $char
*
* @param string $address email (Ex: "toto@examle.com". Long form "John Do <johndo@example.com>" will be false)
* @param int $acceptsupervisorkey If 1, the special string '__SUPERVISOREMAIL__' is also accepted as valid
* @param int $acceptuserkey If 1, the special string '__USER_EMAIL__' is also accepted as valid
* @return boolean true if email syntax is OK, false if KO or empty string
* @see isValidMXRecord()
*/
function isValidEmail($address, $acceptsupervisorkey = 0)
function isValidEmail($address, $acceptsupervisorkey = 0, $acceptuserkey = 0)
{
if ($acceptsupervisorkey && $address == '__SUPERVISOREMAIL__') {
return true;
}
if ($acceptuserkey && $address == '__USER_EMAIL__') {
return true;
}
if (filter_var($address, FILTER_VALIDATE_EMAIL)) {
return true;
}