From 2218ad7d60ec68726bda1e59e6d1b95747caeaad Mon Sep 17 00:00:00 2001
From: alejandro-campos <alejandro.campos@artica.es>
Date: Thu, 12 Sep 2019 15:37:16 +0200
Subject: [PATCH] Added email check functionality

---
 .../godmode/setup/setup_general.php           | 74 ++++++++++++++++++-
 1 file changed, 73 insertions(+), 1 deletion(-)

diff --git a/pandora_console/godmode/setup/setup_general.php b/pandora_console/godmode/setup/setup_general.php
index 40678ee9c5..1385a9f193 100644
--- a/pandora_console/godmode/setup/setup_general.php
+++ b/pandora_console/godmode/setup/setup_general.php
@@ -58,6 +58,16 @@ 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]);
+
+    echo $res;
+}
+
 $table = new StdClass();
 $table->class = 'databox filters';
 $table->id = 'setup_general';
@@ -348,7 +358,7 @@ echo '</fieldset>';
 echo '<fieldset>';
 echo '<legend>'.__('Mail configuration').'</legend>';
 
-$table_mail_conf->data[0][0] = __('From dir');
+$table_mail_conf->data[0][0] = __('From address');
 $table_mail_conf->data[0][1] = html_print_input_text('email_from_dir', $config['email_from_dir'], '', 30, 100, true);
 
 $table_mail_conf->data[1][0] = __('From name');
@@ -369,9 +379,16 @@ $table_mail_conf->data[5][1] = html_print_input_text('email_username', $config['
 $table_mail_conf->data[6][0] = __('Email password');
 $table_mail_conf->data[6][1] = html_print_input_password('email_password', io_output_password($config['email_password']), '', 30, 100, true);
 
+$uniqid = uniqid();
+
+$table_mail_conf->data[7][0] = html_print_button(__('Email test'), 'email_test_dialog', false, "show_email_test('$uniqid');", 'class="sub next"', true).ui_print_help_tip(__('Check the current saved email configuration by sending a test email to a desired account.'), true);
+
+print_email_test_modal_window($uniqid);
+
 html_print_input_hidden('update_config', 1);
 html_print_table($table_mail_conf);
 
+
 echo '</fieldset>';
 
 echo '<div class="action-buttons" style="width: '.$table->width.'">';
@@ -379,6 +396,25 @@ html_print_submit_button(__('Update'), 'update_button', false, 'class="sub upd"'
 echo '</div>';
 echo '</form>';
 
+// Print the modal window for the summary of each alerts group
+function print_email_test_modal_window($id)
+{
+    // Email config table.
+    $table_mail_test = new stdClass();
+    $table_mail_test->width = '100%';
+    $table_mail_test->class = 'databox filters';
+    $table_mail_test->data = [];
+    $table_mail_test->style[0] = 'font-weight: bold';
+    $table_mail_test->colspan[1][0] = 2;
+
+    $table_mail_test->data[0][0] = __('Address').ui_print_help_tip(__('Email address to which the test email will be sent. Please check your inbox after email is sent.'), true);
+    $table_mail_test->data[0][1] = html_print_input_text('email_test_address', '', '', 40, 100, true);
+
+    $table_mail_test->data[1][0] = html_print_button(__('Send'), 'email_test', false, '', 'class="sub next"', true).'&nbsp&nbsp<span id="email_test_sent_message" style="display:none;">Email sent</span><span id="email_test_failure_message" style="display:none;">Email could not been sent</span>';
+
+    echo '<div id="email_test_'.$id.'" title="'.__('Check mail configuration').'" style="display:none">'.html_print_table($table_mail_test, true).'</div>';
+}
+
 
 ?>
 <script type="text/javascript">
@@ -417,6 +453,40 @@ function show_timezone () {
     });
 }
 
+function show_email_test(id) {
+    $('#email_test_sent_message').hide();
+    $('#email_test_failure_message').hide();
+
+    $("#email_test_"+id).dialog({
+        resizable: true,
+        draggable: true,
+        modal: true,
+        height: 175,
+        width: 450,
+        overlay: {
+            opacity: 0.5,
+            background: "black"
+        }
+    });
+}
+
+function perform_email_test () {
+    var test_address = $('#text-email_test_address').val();
+
+    $.ajax({
+        type: "POST",
+        url: "ajax.php",
+        data: "page=godmode/setup/setup_general&test_address="+test_address,
+        dataType: "html",
+        success: function(data) {
+            $('#email_test_sent_message').show();
+        },
+        error: function() {
+            $('#email_test_failure_message').show();
+        },
+    });
+}
+
 $(document).ready (function () {
 
     $("#zone").attr("hidden", true);
@@ -473,5 +543,7 @@ $(document).ready (function () {
         });
         }
     })
+
+    $('input#button-email_test').click(perform_email_test);
 });
 </script>