From e08a3dd04055feb7022d7884350110e88fd1e5d9 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 31 Aug 2017 16:28:18 +0200 Subject: [PATCH] Core: Send _data_ macro as image when needed --- pandora_server/lib/PandoraFMS/Core.pm | 51 ++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 54d96a0a60..b317fe1da5 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -1087,6 +1087,21 @@ sub pandora_execute_action ($$$$$$$$$;$) { # Email } elsif ($clean_name eq "eMail") { + + my $attach_data_as_image = 0; + + my $cid_data = "CID_IMAGE"; + my $dataname = "CID_IMAGE.png"; + + if ($data =~ /^data:image\/png;base64, /) { + # macro _data_ substitution in case is image. + $attach_data_as_image = 1; + my $_cid = ''; + + $field3 =~ s/_data_/$_cid/g; + } + + # Address $field1 = subst_alert_macros ($field1, \%macros, $pa_config, $dbh, $agent, $module); # Subject @@ -1127,10 +1142,10 @@ sub pandora_execute_action ($$$$$$$$$;$) { return ''; } } - + return ''; }; - + # Macro data may contain HTML entities eval { no warnings; @@ -1148,10 +1163,14 @@ sub pandora_execute_action ($$$$$$$$$;$) { $content_type = 'text/html; charset="UTF-8"'; } + + my $boundary = "====" . time() . "===="; + my $html_content_type = $content_type; + # Build the mail with attached content - if (keys(%{$module_graph_list}) > 0) { - my $boundary = "====" . time() . "===="; - my $html_content_type = $content_type; + if ((keys(%{$module_graph_list}) > 0) && ($attach_data_as_image == 0)) { + # module_graph only available if data is NOT an image + $content_type = 'multipart/related; boundary="'.$boundary.'"'; $boundary = "--" . $boundary; @@ -1178,6 +1197,28 @@ sub pandora_execute_action ($$$$$$$$$;$) { $field3 .= $boundary . "--\n"; } + + if ($attach_data_as_image == 1) { + # it's an image in base64! + + $content_type = 'multipart/related; boundary="'.$boundary.'"'; + $boundary = "--" . $boundary; + + my $base64_data = substr($data, 23); # remove first 23 characters: 'data:image/png;base64, ' + + $field3 = $boundary . "\n" + . "Content-Type: " . $html_content_type . "\n\n" + #. "Content-Transfer-Encoding: quoted-printable\n\n" + . $field3 . "\n"; + + $field3 .= $boundary . "\n" + . "Content-Type: image/png; name=\"" . $dataname . "\"\n" + . "Content-Disposition: inline; filename=\"" . $dataname . "\"\n" + . "Content-Transfer-Encoding: base64\n" + . "Content-ID: <" . $cid_data . ">\n" + . "Content-Location: " . $dataname . "\n\n" + . $base64_data . "\n"; + } if ($pa_config->{"mail_in_separate"} != 0){ foreach my $address (split (',', $field1)) {