Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
}
}
}
Expand Down
74 changes: 74 additions & 0 deletions tests/Unit/TholdMailSeparateRecipientsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

/**
* "Send Emails Separately" reaches every address on the list (#754).
*
* The list was split on commas without trimming, so " noc@example.org" failed
* FILTER_VALIDATE_EMAIL and was skipped, with nothing logged.
*/
final class TholdMailSeparateRecipientsTest extends TestCase {
/**
* @return void
*/
public static function setUpBeforeClass(): void {
self::loadPluginSource('includes/functions.php');
}

/**
* @return void
*/
protected function setUp(): void {
parent::setUp();

CactiStubs::$configOptions['thold_from_email'] = 'cacti@example.org';
CactiStubs::$configOptions['thold_from_name'] = 'Cacti';
CactiStubs::$configOptions['thold_send_email_separately'] = 'on';
}

/**
* @param string $to
*
* @return void
*/
private function send($to) {
$host = ['id' => 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);
}
}
Loading