Skip to content

Commit

Permalink
SendMail: Send PDFs asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab authored and raviks789 committed May 7, 2024
1 parent 6fb47e3 commit 857188e
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions library/Reporting/Actions/SendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,7 @@
use ipl\Stdlib\Str;
use ipl\Validator\CallbackValidator;
use ipl\Validator\EmailAddressValidator;
use Throwable;

class SendMail extends ActionHook
{
Expand Down Expand Up @@ -40,11 +42,25 @@ public function execute(Report $report, array $config)
$mail->setSubject($config['subject']);
}

/** @var array<int, string> $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);

Expand All @@ -57,10 +73,7 @@ public function execute(Report $report, array $config)
throw new \InvalidArgumentException();
}

/** @var array<int, string> $recipients */
$recipients = preg_split('/[\s,]+/', $config['recipients']);

$mail->send(null, array_filter($recipients));
$mail->send(null, $recipients);
}

public function initConfigForm(Form $form, Report $report)
Expand Down

0 comments on commit 857188e

Please sign in to comment.