System: Do not send mails to inactive users - refs BT#21146 #4958

pull/4990/head
Yannick Warnier 2 years ago
parent bae62a895b
commit 2d993de894
  1. 12
      main/admin/email_tester.php
  2. 18
      main/inc/lib/api.lib.php
  3. 5
      main/inc/lib/message.lib.php
  4. 52
      main/inc/lib/usermanager.lib.php

@ -50,9 +50,15 @@ if ($form->validate()) {
(!empty(api_get_mail_configuration_value('SMTP_UNIQUE_SENDER')) ? api_get_mail_configuration_value('SMTP_FROM_EMAIL') : $user->getEmail())
);
Display::addFlash(
Display::return_message(get_lang('MailingTestSent'), 'success')
);
if ($mailIsSent) {
Display::addFlash(
Display::return_message(get_lang('MailingTestSent'), 'success')
);
} else {
Display::addFlash(
Display::return_message(get_lang('MailingTestNotSent'), 'error')
);
}
header('Location: '.api_get_self());
exit;

@ -9710,16 +9710,30 @@ function api_mail_html(
if (is_array($recipient_email)) {
foreach ($recipient_email as $dest) {
if (api_valid_email($dest)) {
$mail->AddAddress($dest, $recipient_name);
if (UserManager::isEmailingAllowed($dest)) {
// Do not send if user is not active = 1
$mail->AddAddress($dest, $recipient_name);
}
} else {
// error_log('e-mail recipient '.$dest.' is not valid.');
return 0;
}
}
} else {
if (api_valid_email($recipient_email)) {
$mail->AddAddress($recipient_email, $recipient_name);
if (UserManager::isEmailingAllowed($recipient_email)) {
// Do not send if user is not active = 1
$mail->AddAddress($recipient_email, $recipient_name);
}
} else {
// error_log('e-mail recipient '.$recipient_email.' is not valid.');
return 0;
}
}
if (empty($mail->getAllRecipientAddresses())) {
// error_log('No valid and active destination e-mail in api_mail_html() with address '.print_r($recipient_email, 1).'. Not sending.');
return 0;
}
if (is_array($extra_headers) && count($extra_headers) > 0) {
foreach ($extra_headers as $key => $value) {

@ -553,11 +553,6 @@ class MessageManager
return false;
}
// Disabling messages for inactive users.
if (0 == $receiverUserInfo['active']) {
return false;
}
// Disabling messages depending the pausetraining plugin.
$allowPauseFormation =
'true' === api_get_plugin_setting('pausetraining', 'tool_enable') &&

@ -8073,4 +8073,56 @@ SQL;
return $url;
}
/**
* Get a list of users with the given e-mail address + their "active" field value (0 or 1)
*
* @param string $mail User id
*
* @return array List of users e-mails + active field
*/
public static function getUsersByMail(string $mail): array
{
$resultData = Database::select(
'id, active',
Database::get_main_table(TABLE_MAIN_USER),
[
'where' => ['email = ?' => $mail],
],
'all',
null,
true
);
if ($resultData === false) {
return [];
}
return $resultData;
}
/**
* Get whether we can send an e-mail or not.
* If the e-mail is not in the database, send the mail.
* If the e-mail is in the database but none of its occurences is active, don't send.
* @param string $mail The e-mail address to check
* @return bool Whether we can send an e-mail or not
*/
public function isEmailingAllowed(string $mail): bool
{
$list = self::getUsersByMail($mail);
if (empty($list)) {
// No e-mail matches, send the mail
return true;
}
$send = false;
foreach ($list as $id => $user) {
if ($user['active'] == 1) {
// as soon as we find at least one active user, send the mail
return true;
}
}
return false;
}
}

Loading…
Cancel
Save