From 2915a3bd3566bd554cc529e5af41fd4c5e39d822 Mon Sep 17 00:00:00 2001 From: alejandro-campos Date: Tue, 6 Oct 2020 11:07:46 +0200 Subject: [PATCH] fixed email test --- .../godmode/setup/setup_general.php | 19 +++---- pandora_console/include/functions.php | 57 +++++++++++++++++++ 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/pandora_console/godmode/setup/setup_general.php b/pandora_console/godmode/setup/setup_general.php index 0d8ce68e8e..9a5c3b96c3 100644 --- a/pandora_console/godmode/setup/setup_general.php +++ b/pandora_console/godmode/setup/setup_general.php @@ -59,18 +59,10 @@ global $config; check_login(); if (is_ajax()) { - enterprise_include_once('include/functions_cron.php'); - $test_address = get_parameter('test_address', ''); - $res = enterprise_hook( - 'send_email_attachment', - [ - $test_address, - __('This is an email test sent from Pandora FMS. If you can read this, your configuration works.'), - __('Testing Pandora FMS email'), - null, - ] + $res = send_test_email( + $test_address ); echo $res; @@ -469,7 +461,6 @@ function show_email_test(id) { resizable: true, draggable: true, modal: true, - height: 175, width: 450, overlay: { opacity: 0.5, @@ -487,7 +478,11 @@ function perform_email_test () { data: "page=godmode/setup/setup_general&test_address="+test_address, dataType: "html", success: function(data) { - $('#email_test_sent_message').show(); + if (parseInt(data) === 1) { + $('#email_test_sent_message').show(); + } else { + $('#email_test_failure_message').show(); + } }, error: function() { $('#email_test_failure_message').show(); diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php index 9485d39fc1..f78f4d385e 100644 --- a/pandora_console/include/functions.php +++ b/pandora_console/include/functions.php @@ -5735,3 +5735,60 @@ function get_data_multiplier($unit) return $multiplier; } + + +/** + * Send test email to check email setups. + * + * @param string $to Target email account. + * + * @return integer Status of the email send task. + */ +function send_test_email( + string $to +) { + global $config; + + $result = false; + try { + $transport = new Swift_SmtpTransport( + $config['email_smtpServer'], + $config['email_smtpPort'] + ); + + $transport->setUsername($config['email_username']); + $transport->setPassword($config['email_password']); + + if ($config['email_encryption']) { + $transport->setEncryption($config['email_encryption']); + } + + $mailer = new Swift_Mailer($transport); + + $message = new Swift_Message(io_safe_output(__('Testing Pandora FMS email'))); + + $message->setFrom( + [ + $config['email_from_dir'] => io_safe_output( + $config['email_from_name'] + ), + ] + ); + + $to = trim($to); + $message->setTo([$to => $to]); + $message->setBody( + __('This is an email test sent from Pandora FMS. If you can read this, your configuration works.'), + 'text/html' + ); + + ini_restore('sendmail_from'); + + $result = $mailer->send($message); + } catch (Exception $e) { + error_log($e->getMessage()); + db_pandora_audit('Cron jobs mail', $e->getMessage()); + } + + return $result; +}