Merge branch 'ent-6432-No-funciona-el-envio-de-mails-by-default' into 'develop'

fixed email test

See merge request artica/pandorafms!3504
This commit is contained in:
Daniel Rodriguez 2020-10-08 20:01:33 +02:00
commit b6ee1a9434
2 changed files with 64 additions and 12 deletions

View File

@ -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;
@ -479,7 +471,6 @@ function show_email_test(id) {
resizable: true,
draggable: true,
modal: true,
height: 175,
width: 450,
overlay: {
opacity: 0.5,
@ -497,7 +488,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();

View File

@ -5740,3 +5740,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;
}