From 2969f6791379a37b0a11454810c8283ce34993d4 Mon Sep 17 00:00:00 2001 From: Sean Mancini Date: Thu, 5 Nov 2026 23:47:52 -0500 Subject: [PATCH] fix(mail): trim recipients when sending emails separately (#754) With "Send Emails Separately" enabled, thold_mail() split the recipient list on commas without trimming. Lists are commonly joined with ", ", so every address after the first (" noc@example.org") failed FILTER_VALIDATE_EMAIL and was skipped without any log entry. A trailing comma produced an empty entry, and since filter_var('') returns false and false == '' in PHP, mailer() was even called with an empty address. Trim each address and drop empty entries, and log any address that is still invalid instead of skipping it silently. --- includes/functions.php | 9 ++- .../Unit/TholdMailSeparateRecipientsTest.php | 74 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/TholdMailSeparateRecipientsTest.php diff --git a/includes/functions.php b/includes/functions.php index 1504bac3..3cf14d39 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -8487,7 +8487,12 @@ function thold_mail($to_email, $bcc_email, $from_email, $subject, $message, $fil ); } } else { - $ar_to_email = explode(',', $to_email); + /** + * Lists are joined with ", " and often end in a stray comma, so trim + * each address and drop the empty ones. Untrimmed, " b@example.com" + * fails validation and was skipped without a word (#754). + */ + $ar_to_email = array_filter(array_map('trim', explode(',', $to_email)), 'strlen'); foreach ($ar_to_email as $to) { if (filter_var($to, FILTER_VALIDATE_EMAIL) == $to) { @@ -8527,6 +8532,8 @@ function thold_mail($to_email, $bcc_email, $from_email, $subject, $message, $fil $any_error = $error; } } + } else { + cacti_log("WARNING: Not sending Email to invalid address '$to' Subject:$subject", true, 'THOLD'); } } } diff --git a/tests/Unit/TholdMailSeparateRecipientsTest.php b/tests/Unit/TholdMailSeparateRecipientsTest.php new file mode 100644 index 00000000..47e6a4bb --- /dev/null +++ b/tests/Unit/TholdMailSeparateRecipientsTest.php @@ -0,0 +1,74 @@ + 2]; + + thold_mail($to, '', '', 'ALERT: CPU', 'CPU is high', [], [], 0, $host); + } + + /** + * @return void + */ + public function testAddressesAfterACommaAndSpaceAreMailed(): void { + $this->send('ops@example.org, noc@example.org, oncall@example.org ,'); + + $this->assertSame( + ['ops@example.org', 'noc@example.org', 'oncall@example.org'], + array_column(CactiStubs::$mail, 'to') + ); + } + + /** + * @return void + */ + public function testAnInvalidAddressIsLoggedNotSilentlyDropped(): void { + $this->send('ops@example.org, not-an-address'); + + $this->assertSame(['ops@example.org'], array_column(CactiStubs::$mail, 'to')); + $this->assertContains("WARNING: Not sending Email to invalid address 'not-an-address' Subject:ALERT: CPU", CactiStubs::$log); + } +}