Skip to content

Commit

Permalink
Catch more mail exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
murrant committed Nov 4, 2024
1 parent 338fa74 commit 1ce2dd2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
8 changes: 5 additions & 3 deletions LibreNMS/IRCBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,12 @@ private function _auth($params)
$this->log("Auth for '" . $params[0] . "', ID: '" . $user->user_id . "', Token: '" . $token . "', Mail: '" . $user->email . "'");
}

if (Mail::send($user->email, 'LibreNMS IRC-Bot Authtoken', "Your Authtoken for the IRC-Bot:\r\n\r\n" . $token . "\r\n\r\n", false) === true) {
try {
Mail::send($user->email, 'LibreNMS IRC-Bot Authtoken', "Your Authtoken for the IRC-Bot:\r\n\r\n" . $token . "\r\n\r\n");

return $this->respond('Token sent!');
} else {
return $this->respond('Sorry, seems like mail doesnt like us.');
} catch (\Exception $e) {
return $this->respond('Sorry, seems like mail doesnt like us. ' . $e->getMessage());
}
} else {
return $this->respond('Who are you again?');
Expand Down
6 changes: 4 additions & 2 deletions LibreNMS/Util/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ public static function parseEmails($emails)
* @param string $subject
* @param string $message
* @param bool $html
* @param bool $bcc
* @param bool|null $embedGraphs
* @return bool
*
* @throws Exception if mail delivery fails.
* @throws \PHPMailer\PHPMailer\Exception
*/
public static function send($emails, $subject, $message, bool $html = false, bool $bcc = false, ?bool $embedGraphs = null)
public static function send($emails, $subject, $message, bool $html = false, bool $bcc = false, ?bool $embedGraphs = null): bool
{
if (is_array($emails) || ($emails = self::parseEmails($emails))) {
d_echo("Attempting to email $subject to: " . implode('; ', array_keys($emails)) . PHP_EOL);
Expand Down
7 changes: 4 additions & 3 deletions LibreNMS/Validations/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ public function validate(Validator $validator): void
}//end if
if ($run_test == 1) {
$email = Config::get('alert.default_mail');
if ($err = \LibreNMS\Util\Mail::send($email, 'Test email', 'Testing email from NMS', false)) {
try {
\LibreNMS\Util\Mail::send($email, 'Test email', 'Testing email from NMS');
$validator->ok('Email has been sent');
} else {
$validator->fail("Issue sending email to $email with error $err");
} catch (\Exception $e) {
$validator->fail("Issue sending email to $email with error " . $e->getMessage());
}
}
}//end if
Expand Down
6 changes: 5 additions & 1 deletion daily.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,11 @@

if ($options['f'] === 'notify') {
if (\LibreNMS\Config::has('alert.default_mail')) {
\LibreNMS\Util\Mail::send(\LibreNMS\Config::get('alert.default_mail'), '[LibreNMS] Auto update has failed for ' . Config::get('distributed_poller_name'), "We just attempted to update your install but failed. The information below should help you fix this.\r\n\r\n" . $options['o'], false);
try {
\LibreNMS\Util\Mail::send(\LibreNMS\Config::get('alert.default_mail'), '[LibreNMS] Auto update has failed for ' . Config::get('distributed_poller_name'), "We just attempted to update your install but failed. The information below should help you fix this.\r\n\r\n" . $options['o'], false);
} catch (Exception $e) {
echo "Failed to send update failed email. " . $e->getMessage();
}
}
}

Expand Down

0 comments on commit 1ce2dd2

Please sign in to comment.