summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Actions
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--library/Reporting/Actions/SendMail.php40
1 files changed, 31 insertions, 9 deletions
diff --git a/library/Reporting/Actions/SendMail.php b/library/Reporting/Actions/SendMail.php
index 7c70bf5..a0dc42f 100644
--- a/library/Reporting/Actions/SendMail.php
+++ b/library/Reporting/Actions/SendMail.php
@@ -1,4 +1,5 @@
<?php
+
// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2
namespace Icinga\Module\Reporting\Actions;
@@ -9,6 +10,9 @@ use Icinga\Module\Reporting\Hook\ActionHook;
use Icinga\Module\Reporting\Mail;
use Icinga\Module\Reporting\Report;
use ipl\Html\Form;
+use ipl\Stdlib\Str;
+use ipl\Validator\CallbackValidator;
+use ipl\Validator\EmailAddressValidator;
class SendMail extends ActionHook
{
@@ -28,7 +32,9 @@ class SendMail extends ActionHook
$mail = new Mail();
- $mail->setFrom(Config::module('reporting')->get('mail', 'from', 'reporting@icinga'));
+ $mail->setFrom(
+ Config::module('reporting', 'config', true)->get('mail', 'from', 'reporting@icinga')
+ );
if (isset($config['subject'])) {
$mail->setSubject($config['subject']);
@@ -51,9 +57,10 @@ class SendMail extends ActionHook
throw new \InvalidArgumentException();
}
- $recipients = array_filter(preg_split('/[\s,]+/', $config['recipients']));
+ /** @var array<int, string> $recipients */
+ $recipients = preg_split('/[\s,]+/', $config['recipients']);
- $mail->send(null, $recipients);
+ $mail->send(null, array_filter($recipients));
}
public function initConfigForm(Form $form, Report $report)
@@ -66,19 +73,34 @@ class SendMail extends ActionHook
}
$form->addElement('select', 'type', [
- 'required' => true,
- 'label' => t('Type'),
- 'options' => $types
+ 'required' => true,
+ 'label' => t('Type'),
+ 'options' => $types
]);
$form->addElement('text', 'subject', [
- 'label' => t('Subject'),
- 'placeholder' => Mail::DEFAULT_SUBJECT
+ 'label' => t('Subject'),
+ 'placeholder' => Mail::DEFAULT_SUBJECT
]);
$form->addElement('textarea', 'recipients', [
'required' => true,
- 'label' => t('Recipients')
+ 'label' => t('Recipients'),
+ 'validators' => [
+ new CallbackValidator(function ($value, CallbackValidator $validator): bool {
+ $mailValidator = new EmailAddressValidator();
+ $mails = Str::trimSplit($value);
+ foreach ($mails as $mail) {
+ if (! $mailValidator->isValid($mail)) {
+ $validator->addMessage(...$mailValidator->getMessages());
+
+ return false;
+ }
+ }
+
+ return true;
+ })
+ ]
]);
}
}