Skip to content

Commit

Permalink
- add dispatch event pre send email
Browse files Browse the repository at this point in the history
- add one propeties skip
- add new propeties and getter setters in emailSendEvent to help to skip of return error when is necessary
  • Loading branch information
lenonleitecience committed Feb 13, 2024
1 parent 0d462fc commit 8db4ffe
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/bundles/EmailBundle/EmailEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
49 changes: 49 additions & 0 deletions app/bundles/EmailBundle/Event/EmailSendEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
35 changes: 34 additions & 1 deletion app/bundles/EmailBundle/Helper/MailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 8db4ffe

Please sign in to comment.