Skip to content

Commit

Permalink
System: Do not send mails to inactive users - refs BT#21146 #4958
Browse files Browse the repository at this point in the history
  • Loading branch information
ywarnier committed Nov 5, 2023
1 parent bae62a8 commit 2d993de
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 deletions.
12 changes: 9 additions & 3 deletions main/admin/email_tester.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@
(!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;
Expand Down
18 changes: 16 additions & 2 deletions main/inc/lib/api.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 0 additions & 5 deletions main/inc/lib/message.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,6 @@ public static function send_message(
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') &&
Expand Down
52 changes: 52 additions & 0 deletions main/inc/lib/usermanager.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -8073,4 +8073,56 @@ private static function getGravatar(

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;
}
}

0 comments on commit 2d993de

Please sign in to comment.