2017-10-26 01:56:09 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Mail.php
|
|
|
|
*
|
|
|
|
* -Description-
|
|
|
|
*
|
|
|
|
* 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-10-26 01:56:09 -05:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2017-10-26 01:56:09 -05:00
|
|
|
* @copyright 2017 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace LibreNMS\Validations;
|
|
|
|
|
|
|
|
use LibreNMS\Config;
|
|
|
|
use LibreNMS\Validator;
|
|
|
|
|
2018-02-27 09:57:20 -06:00
|
|
|
class Mail extends BaseValidation
|
2017-10-26 01:56:09 -05:00
|
|
|
{
|
2018-02-27 09:57:20 -06:00
|
|
|
protected static $RUN_BY_DEFAULT = false;
|
2017-10-26 01:56:09 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate this module.
|
|
|
|
* To return ValidationResults, call ok, warn, fail, or result methods on the $validator
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param Validator $validator
|
2017-10-26 01:56:09 -05:00
|
|
|
*/
|
2022-06-10 16:25:33 -05:00
|
|
|
public function validate(Validator $validator): void
|
2017-10-26 01:56:09 -05:00
|
|
|
{
|
|
|
|
if (Config::get('alert.transports.mail') === true) {
|
|
|
|
$run_test = 1;
|
2020-09-21 14:54:51 +02:00
|
|
|
if (! Config::has('alert.default_mail')) {
|
2017-10-26 01:56:09 -05:00
|
|
|
$validator->fail('default_mail config option needs to be specified to test email');
|
|
|
|
$run_test = 0;
|
|
|
|
} elseif (Config::get('email_backend') == 'sendmail') {
|
2020-09-21 14:54:51 +02:00
|
|
|
if (! Config::has('email_sendmail_path')) {
|
2020-09-21 15:59:34 +02:00
|
|
|
$validator->fail('You have selected sendmail but not configured email_sendmail_path');
|
2017-10-26 01:56:09 -05:00
|
|
|
$run_test = 0;
|
2020-09-21 14:54:51 +02:00
|
|
|
} elseif (! file_exists(Config::get('email_sendmail_path'))) {
|
2020-09-21 15:59:34 +02:00
|
|
|
$validator->fail('The configured email_sendmail_path is not valid');
|
2017-10-26 01:56:09 -05:00
|
|
|
$run_test = 0;
|
|
|
|
}
|
|
|
|
} elseif (Config::get('email_backend') == 'smtp') {
|
2020-09-21 14:54:51 +02:00
|
|
|
if (! Config::has('email_smtp_host')) {
|
2017-10-26 01:56:09 -05:00
|
|
|
$validator->fail('You have selected SMTP but not configured an SMTP host');
|
|
|
|
$run_test = 0;
|
|
|
|
}
|
2020-09-21 14:54:51 +02:00
|
|
|
if (! Config::has('email_smtp_port')) {
|
2017-10-26 01:56:09 -05:00
|
|
|
$validator->fail('You have selected SMTP but not configured an SMTP port');
|
|
|
|
$run_test = 0;
|
|
|
|
}
|
|
|
|
if (Config::get('email_smtp_auth')
|
2020-09-21 14:54:51 +02:00
|
|
|
&& (! Config::has('email_smtp_username') || ! Config::has('email_smtp_password'))
|
2017-10-26 01:56:09 -05:00
|
|
|
) {
|
|
|
|
$validator->fail('You have selected SMTP auth but have not configured both username and password');
|
|
|
|
$run_test = 0;
|
|
|
|
}
|
|
|
|
}//end if
|
|
|
|
if ($run_test == 1) {
|
|
|
|
$email = Config::get('alert.default_mail');
|
|
|
|
if ($err = send_mail($email, 'Test email', 'Testing email from NMS')) {
|
|
|
|
$validator->ok('Email has been sent');
|
|
|
|
} else {
|
|
|
|
$validator->fail("Issue sending email to $email with error $err");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}//end if
|
|
|
|
}
|
|
|
|
}
|