summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Actions/SendMail.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:28:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:28:59 +0000
commit978a1651bac3faf5e91ddba327bf39aeb6a4cb63 (patch)
treef0d1fee61877df200ccfb1c0af58a39cd551fb46 /library/Reporting/Actions/SendMail.php
parentInitial commit. (diff)
downloadicingaweb2-module-reporting-978a1651bac3faf5e91ddba327bf39aeb6a4cb63.tar.xz
icingaweb2-module-reporting-978a1651bac3faf5e91ddba327bf39aeb6a4cb63.zip
Adding upstream version 0.10.0.upstream/0.10.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Reporting/Actions/SendMail.php')
-rw-r--r--library/Reporting/Actions/SendMail.php84
1 files changed, 84 insertions, 0 deletions
diff --git a/library/Reporting/Actions/SendMail.php b/library/Reporting/Actions/SendMail.php
new file mode 100644
index 0000000..7c70bf5
--- /dev/null
+++ b/library/Reporting/Actions/SendMail.php
@@ -0,0 +1,84 @@
+<?php
+// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2
+
+namespace Icinga\Module\Reporting\Actions;
+
+use Icinga\Application\Config;
+use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport;
+use Icinga\Module\Reporting\Hook\ActionHook;
+use Icinga\Module\Reporting\Mail;
+use Icinga\Module\Reporting\Report;
+use ipl\Html\Form;
+
+class SendMail extends ActionHook
+{
+ public function getName()
+ {
+ return 'Send Mail';
+ }
+
+ public function execute(Report $report, array $config)
+ {
+ $name = sprintf(
+ '%s (%s) %s',
+ $report->getName(),
+ $report->getTimeframe()->getName(),
+ date('Y-m-d H:i')
+ );
+
+ $mail = new Mail();
+
+ $mail->setFrom(Config::module('reporting')->get('mail', 'from', 'reporting@icinga'));
+
+ if (isset($config['subject'])) {
+ $mail->setSubject($config['subject']);
+ }
+
+ switch ($config['type']) {
+ case 'pdf':
+ $mail->attachPdf(Pdfexport::first()->htmlToPdf($report->toPdf()), $name);
+
+ break;
+ case 'csv':
+ $mail->attachCsv($report->toCsv(), $name);
+
+ break;
+ case 'json':
+ $mail->attachJson($report->toJson(), $name);
+
+ break;
+ default:
+ throw new \InvalidArgumentException();
+ }
+
+ $recipients = array_filter(preg_split('/[\s,]+/', $config['recipients']));
+
+ $mail->send(null, $recipients);
+ }
+
+ public function initConfigForm(Form $form, Report $report)
+ {
+ $types = ['pdf' => 'PDF'];
+
+ if ($report->providesData()) {
+ $types['csv'] = 'CSV';
+ $types['json'] = 'JSON';
+ }
+
+ $form->addElement('select', 'type', [
+ 'required' => true,
+ 'label' => t('Type'),
+ 'options' => $types
+ ]);
+
+ $form->addElement('text', 'subject', [
+ 'label' => t('Subject'),
+ 'placeholder' => Mail::DEFAULT_SUBJECT
+ ]);
+
+ $form->addElement('textarea', 'recipients', [
+ 'required' => true,
+ 'label' => t('Recipients')
+ ]);
+ }
+}