From ccfab497c5b76c477e1c953a2053d85025acca5f Mon Sep 17 00:00:00 2001 From: lenonleite Date: Fri, 9 Feb 2024 10:01:57 +0000 Subject: [PATCH] - add fail to email. --- .../EmailBundle/Event/EmailSendEvent.php | 20 +++++++++++++++++-- .../EventListener/BuilderSubscriber.php | 4 +++- app/bundles/EmailBundle/Helper/MailHelper.php | 12 ++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/bundles/EmailBundle/Event/EmailSendEvent.php b/app/bundles/EmailBundle/Event/EmailSendEvent.php index e87e123dddb..34155343784 100644 --- a/app/bundles/EmailBundle/Event/EmailSendEvent.php +++ b/app/bundles/EmailBundle/Event/EmailSendEvent.php @@ -68,6 +68,7 @@ public function __construct( $this->canSend = $args['canSend'] ?? true; $this->errors = $args['errors'] ?? []; $this->fatal = $args['fatal'] ?? false; + $this->skip = $args['skip'] ?? false; if (!$this->subject && $this->email instanceof Email) { $this->subject = $args['email']->getSubject(); @@ -336,15 +337,30 @@ public function getCombinedContent(): string public function enableSend() { - $this->fatal = false; + $this->skip = false; } public function disableSend() { - $this->fatal = true; + $this->skip = true; } public function isEnable(): bool + { + return $this->skip; + } + + public function setFatal(): void + { + $this->fatal = true; + } + + public function setNotFatal() + { + $this->fatal = false; + } + + public function isFatal(): bool { return $this->fatal; } diff --git a/app/bundles/EmailBundle/EventListener/BuilderSubscriber.php b/app/bundles/EmailBundle/EventListener/BuilderSubscriber.php index c1010162bd8..709d03db57a 100644 --- a/app/bundles/EmailBundle/EventListener/BuilderSubscriber.php +++ b/app/bundles/EmailBundle/EventListener/BuilderSubscriber.php @@ -82,7 +82,9 @@ public function emailPreSend(EmailSendEvent $event): void ] ); if (!empty($result) && count($result) > 1) { - $event->addError('Error Email already sent to this lead'); + $emailStatToChange = end($result); + $emailStatToChange->setIsFailed(true); + $this->emailModel->getStatRepository()->saveEntity($emailStatToChange); $event->disableSend(); } } diff --git a/app/bundles/EmailBundle/Helper/MailHelper.php b/app/bundles/EmailBundle/Helper/MailHelper.php index 5344432ae06..45af797534f 100644 --- a/app/bundles/EmailBundle/Helper/MailHelper.php +++ b/app/bundles/EmailBundle/Helper/MailHelper.php @@ -214,6 +214,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. */ @@ -334,7 +336,6 @@ 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 @@ -397,7 +398,11 @@ public function send($dispatchSendEvent = false, $isQueueFlush = false) } try { - $this->mailer->send($this->message); + $this->dispatchPreSendEvent(); + 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 @@ -1509,7 +1514,8 @@ public function dispatchPreSendEvent(): void $this->dispatcher->dispatch($event, EmailEvents::EMAIL_PRE_SEND); - $this->fatal = $event->isEnable(); + $this->skip = $event->isEnable(); + $this->fatal = $event->isFatal(); $errors = $event->getErrors(); if (!empty($errors)) { $currentErrors = [];