From 9cebc42a42754a7e06b985afe73bf5bedb025526 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 13 Mar 2024 11:20:24 +0100 Subject: [PATCH] SendMail: Send PDFs asynchronously --- library/Reporting/Actions/SendMail.php | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/library/Reporting/Actions/SendMail.php b/library/Reporting/Actions/SendMail.php index a0dc42f..64f9d4b 100644 --- a/library/Reporting/Actions/SendMail.php +++ b/library/Reporting/Actions/SendMail.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Reporting\Actions; use Icinga\Application\Config; +use Icinga\Application\Logger; use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport; use Icinga\Module\Reporting\Hook\ActionHook; use Icinga\Module\Reporting\Mail; @@ -13,6 +14,7 @@ use ipl\Stdlib\Str; use ipl\Validator\CallbackValidator; use ipl\Validator\EmailAddressValidator; +use Throwable; class SendMail extends ActionHook { @@ -40,11 +42,25 @@ public function execute(Report $report, array $config) $mail->setSubject($config['subject']); } + /** @var array $recipients */ + $recipients = preg_split('/[\s,]+/', $config['recipients']); + $recipients = array_filter($recipients); + switch ($config['type']) { case 'pdf': - $mail->attachPdf(Pdfexport::first()->htmlToPdf($report->toPdf()), $name); + /** @var Pdfexport $exporter */ + $exporter = Pdfexport::first(); + $exporter->asyncHtmlToPdf($report->toPdf())->then( + function ($pdf) use ($mail, $name, $recipients) { + $mail->attachPdf($pdf, $name); + $mail->send(null, $recipients); + } + )->otherwise(function (Throwable $e) { + Logger::error($e); + Logger::debug($e->getTraceAsString()); + }); - break; + return; case 'csv': $mail->attachCsv($report->toCsv(), $name); @@ -57,10 +73,7 @@ public function execute(Report $report, array $config) throw new \InvalidArgumentException(); } - /** @var array $recipients */ - $recipients = preg_split('/[\s,]+/', $config['recipients']); - - $mail->send(null, array_filter($recipients)); + $mail->send(null, $recipients); } public function initConfigForm(Form $form, Report $report)