From 8db4ffe33e203d04cf64b4b92402bb9a94dd56c3 Mon Sep 17 00:00:00 2001 From: lenonleite Date: Tue, 13 Feb 2024 11:29:51 +0000 Subject: [PATCH] - add dispatch event pre send email - add one propeties skip - add new propeties and getter setters in emailSendEvent to help to skip of return error when is necessary --- app/bundles/EmailBundle/EmailEvents.php | 10 ++++ .../EmailBundle/Event/EmailSendEvent.php | 49 +++++++++++++++++++ app/bundles/EmailBundle/Helper/MailHelper.php | 35 ++++++++++++- 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/app/bundles/EmailBundle/EmailEvents.php b/app/bundles/EmailBundle/EmailEvents.php index 1b1d22f8076..134e14034ff 100644 --- a/app/bundles/EmailBundle/EmailEvents.php +++ b/app/bundles/EmailBundle/EmailEvents.php @@ -47,6 +47,16 @@ final class EmailEvents */ public const EMAIL_ON_SEND = 'mautic.email_on_send'; + /** + * The mautic.email_pre_send event is dispatched when an email is clicked. + * + * The event listener receives a + * Mautic\EmailBundle\Event\EmailSendEvent instance. + * + * @var string + */ + public const EMAIL_PRE_SEND = 'mautic.email_pre_send'; + /** * The mautic.email_on_display event is dispatched when an email is viewed via a browser. * diff --git a/app/bundles/EmailBundle/Event/EmailSendEvent.php b/app/bundles/EmailBundle/Event/EmailSendEvent.php index 4104ee6c141..0c3ae459781 100644 --- a/app/bundles/EmailBundle/Event/EmailSendEvent.php +++ b/app/bundles/EmailBundle/Event/EmailSendEvent.php @@ -45,6 +45,12 @@ class EmailSendEvent extends CommonEvent private array $textHeaders = []; + private bool $fatal = false; + + private bool $skip = false; + + private array $errors = []; + /** * @param array $args * @param bool $isDynamicContentParsing @@ -63,6 +69,9 @@ public function __construct( $this->source = $args['source'] ?? []; $this->tokens = $args['tokens'] ?? []; $this->textHeaders = $args['textHeaders'] ?? []; + $this->errors = $args['errors'] ?? []; + $this->fatal = $args['fatal'] ?? false; + $this->skip = $args['skip'] ?? false; if (!$this->subject && $this->email instanceof Email) { $this->subject = (string) $args['email']->getSubject(); @@ -328,4 +337,44 @@ public function getCombinedContent(): string return $content.implode(' ', $this->getTextHeaders()); } + + public function disableSkip() + { + $this->skip = false; + } + + public function enableSkip() + { + $this->skip = true; + } + + public function isSkip(): bool + { + return $this->skip; + } + + public function enableFatal(): void + { + $this->fatal = true; + } + + public function disableFatal() + { + $this->fatal = false; + } + + public function isFatal(): bool + { + return $this->fatal; + } + + public function addError($error): void + { + $this->errors[] = $error; + } + + public function getErrors(): array + { + return $this->errors; + } } diff --git a/app/bundles/EmailBundle/Helper/MailHelper.php b/app/bundles/EmailBundle/Helper/MailHelper.php index 54cf1b094ef..b7be50d0716 100644 --- a/app/bundles/EmailBundle/Helper/MailHelper.php +++ b/app/bundles/EmailBundle/Helper/MailHelper.php @@ -211,6 +211,8 @@ class MailHelper */ protected $fatal = false; + protected bool $skip = false; + /** * Simply a md5 of the content so that event listeners can easily determine if the content has been changed. */ @@ -311,6 +313,7 @@ public function send($dispatchSendEvent = false, $isQueueFlush = false) $this->message->returnPath($this->returnPath); } + $this->dispatchPreSendEvent(); if (empty($this->fatal)) { if (!$isQueueFlush) { // Search/replace tokens if this is not a queue flush @@ -372,7 +375,10 @@ public function send($dispatchSendEvent = false, $isQueueFlush = false) } try { - $this->mailer->send($this->message); + if (!$this->skip) { + $this->mailer->send($this->message); + } + $this->skip = false; } catch (TransportExceptionInterface $exception) { /* The nature of symfony/mailer is working with transactional emails only @@ -2114,4 +2120,31 @@ private function getSystemFrom(): AddressDTO return $this->systemFrom; } + + public function dispatchPreSendEvent(): void + { + if (null === $this->dispatcher) { + $this->dispatcher = $this->factory->getDispatcher(); + } + + if (null === $this->dispatcher) { + return; + } + + $event = new EmailSendEvent($this); + $this->dispatcher->dispatch($event, EmailEvents::EMAIL_PRE_SEND); + + $this->skip = $event->isSkip(); + $this->fatal = $event->isFatal(); + $errors = $event->getErrors(); + if (!empty($errors)) { + $currentErrors = []; + if (isset($this->errors['failures']) && is_array($this->errors['failures'])) { + $currentErrors = $this->errors['failures']; + } + $this->errors['failures'] = array_merge($errors, $currentErrors); + } + + unset($event); + } }