Skip to content

Commit

Permalink
- add fail to email.
Browse files Browse the repository at this point in the history
  • Loading branch information
lenonleitecience committed Feb 9, 2024
1 parent 09399ca commit ccfab49
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
20 changes: 18 additions & 2 deletions app/bundles/EmailBundle/Event/EmailSendEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion app/bundles/EmailBundle/EventListener/BuilderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
12 changes: 9 additions & 3 deletions app/bundles/EmailBundle/Helper/MailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = [];
Expand Down

0 comments on commit ccfab49

Please sign in to comment.