Skip to content

Commit

Permalink
[FEATURE] Adds Event before sending out testmail
Browse files Browse the repository at this point in the history
This event can be used to deactivate the internal sending of test emails and implement your own logic, e.g. sending test newsletters via a separate queue.

Relates to: https://projekte.in2code.de/issues/61246
  • Loading branch information
dhoffmann1979 committed Jan 25, 2024
1 parent eda7e11 commit bc27dd3
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 12 deletions.
50 changes: 40 additions & 10 deletions Classes/Controller/NewsletterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use In2code\Luxletter\Domain\Service\PreviewUrlService;
use In2code\Luxletter\Domain\Service\QueueService;
use In2code\Luxletter\Domain\Service\ReceiverAnalysisService;
use In2code\Luxletter\Events\AfterTestMailButtonClickedEvent;
use In2code\Luxletter\Exception\AuthenticationFailedException;
use In2code\Luxletter\Mail\TestMail;
use In2code\Luxletter\Utility\BackendUserUtility;
Expand Down Expand Up @@ -225,16 +226,43 @@ public function testMailAjax(ServerRequestInterface $request): ResponseInterface
if (BackendUserUtility::isBackendUserAuthenticated() === false) {
throw new AuthenticationFailedException('You are not authenticated to send mails', 1560872725);
}
$testMail = GeneralUtility::makeInstance(TestMail::class);
$status = $testMail->preflight(
$request->getQueryParams()['origin'],
$request->getQueryParams()['layout'],
(int)$request->getQueryParams()['configuration'],
$request->getQueryParams()['subject'],
$request->getQueryParams()['email']
);
$status = null;

/**
* This event can be used for sending the Testemail with external logic
* @see Documentation/Tech/Events.md
*/
/** @var AfterTestMailButtonClickedEvent $event */
$event = GeneralUtility::makeInstance(AfterTestMailButtonClickedEvent::class);
$event->setRequest($request);
$this->eventDispatcher->dispatch($event);

if (!$event->isTestMailIsSendExternal()) {
$testMail = GeneralUtility::makeInstance(TestMail::class);
$status = $testMail->preflight(
$request->getQueryParams()['origin'],
$request->getQueryParams()['layout'],
(int)$request->getQueryParams()['configuration'],
$request->getQueryParams()['subject'],
$request->getQueryParams()['email']
);
}
$event->setTestMailIsSendExternal(true);
$response = ObjectUtility::getJsonResponse();
$response->getBody()->write(json_encode(['status' => $status]));
$responseData = [
'status' => $status ?? $event->getStatus(),
];
if ($event->isTestMailIsSendExternal()){
$responseData = array_merge(
$responseData,
[
'statusTitle' => $event->getStatusTitle(),
'statusMessage' => $event->getStatusMessage(),
'statusSeverity' => $event->getStatusSeverity(),
]
);
}
$response->getBody()->write(json_encode($responseData, JSON_THROW_ON_ERROR));
return $response;
}

Expand All @@ -261,8 +289,10 @@ public function receiverDetailAjax(ServerRequestInterface $request): ResponseInt
$standaloneView->assignMultiple([
'user' => $user,
'visitor' => $visitorRepository->findOneByFrontenduser($user),
'logs' => $logRepository->findByUser($user),
]);
if ($user !== null) {
$standaloneView->assign('logs', $logRepository->findByUser($user));
}
$response = ObjectUtility::getJsonResponse();
$response->getBody()->write(json_encode(
['html' => $standaloneView->render()]
Expand Down
85 changes: 85 additions & 0 deletions Classes/Events/AfterTestMailButtonClickedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);
namespace In2code\Luxletter\Events;

use Psr\Http\Message\ServerRequestInterface;

final class AfterTestMailButtonClickedEvent
{
const STATUS_SEVERITY_SUCCESS = 'alert-success';
const STATUS_SEVERITY_WARNING = 'alert-warning';
const STATUS_SEVERITY_ERROR = 'alert-danger';

protected bool $testMailIsSendExternal = false;

protected bool $status = false;

protected string $statusTitle = '';

protected string $statusMessage = '';

protected string $statusSeverity = self::STATUS_SEVERITY_ERROR;

protected ServerRequestInterface $request;

public function isTestMailIsSendExternal(): bool
{
return $this->testMailIsSendExternal;
}

public function setTestMailIsSendExternal(bool $testMailIsSendExternal): void
{
$this->testMailIsSendExternal = $testMailIsSendExternal;
}

public function getStatus(): bool
{
return $this->status;
}

public function setStatus(bool $status): void
{
$this->status = $status;
}

public function getRequest(): ServerRequestInterface
{
return $this->request;
}

public function setRequest(ServerRequestInterface $request): void
{
$this->request = $request;
}

public function getStatusTitle(): string
{
return $this->statusTitle;
}

public function setStatusTitle(string $statusTitle): void
{
$this->statusTitle = $statusTitle;
}

public function getStatusMessage(): string
{
return $this->statusMessage;
}

public function setStatusMessage(string $statusMessage): void
{
$this->statusMessage = $statusMessage;
}

public function getStatusSeverity(): string
{
return $this->statusSeverity;
}

public function setStatusSeverity(string $statusSeverity): void
{
$this->statusSeverity = $statusSeverity;
}
}
43 changes: 43 additions & 0 deletions Documentation/Tech/Events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<img align="left" src="../../Resources/Public/Icons/lux.svg" width="50" />

# Luxletter - Email marketing in TYPO3. Send newsletters the easy way.

## Events

There are many events that can be used to extend Luxletter.
This documentation is under construction and not all events are documented yet.

### AfterTestMailButtonClickedEvent

This event can be used to deactivate the internal sending of test emails and implement your own logic, e.g. sending test newsletters via a separate queue.

To deactivate the internal logic, the `$testMailIsSendExternal` property of the event must be set to true.
If the general status `$status` is set to `false`, no message is shown.
If the general status `$status` is set to `true`, a message with the properties `$statusTitle`, `$statusMessage` and `$statusSeverity` is shown.
The values for `$statusSeverity` can be `AfterTestMailButtonClickedEvent::STATUS_SEVERITY_SUCCESS`, `AfterTestMailButtonClickedEvent::STATUS_SEVERITY_WARNING` and `AfterTestMailButtonClickedEvent::STATUS_SEVERITY_ERROR`, the default value is `AfterTestMailButtonClickedEvent::STATUS_SEVERITY_ERROR`.

The `$request` property is available in the event, from which all necessary data can be obtained to send the test e-mail.

Sample Eventlistener:

```
<?php
declare(strict_types=1);
namespace Vendor\Extension\EventListener;
use In2code\Luxletter\Events\AfterTestMailButtonClickedEvent;
final class DemoEventlistener
{
public function __invoke(AfterTestMailButtonClickedEvent $event): void
{
$event->setTestMailIsSendExternal(true);
// ... handle email sending
$event->setStatus(true);
$event->setStatusSeverity(AfterTestMailButtonClickedEvent::STATUS_SEVERITY_SUCCESS);
$event->setStatusTitle('Success');
$event->setStatusMessage('The test email is successfully added to the queue');
}
}
```
8 changes: 7 additions & 1 deletion Resources/Private/Build/JavaScript/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,19 @@ define(['jquery'], function($) {
*/
this.testMailListenerCallback = function(response) {
const messageElement = document.querySelector('[data-luxletter-testmail="message"]');
if (messageElement !== null && response.status === true) {
if (messageElement !== null && response.statusSeverity === undefined && response.status === true) {
showElement(messageElement);

const counterElement = messageElement.querySelector('p>span');
let counter = parseInt(counterElement.innerHTML);
counter++;
counterElement.innerHTML = counter.toString();
} else if(messageElement !== null && response.statusSeverity !== '' && response.status === true) {
messageElement.classList.remove('alert-success');
messageElement.classList.add(response.statusSeverity);
messageElement.querySelector('.alert-heading').innerHTML = response.statusTitle;
messageElement.querySelector('p').innerHTML = response.statusMessage;
showElement(messageElement);
}
};

Expand Down
2 changes: 1 addition & 1 deletion Resources/Public/JavaScript/Luxletter/Module.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bc27dd3

Please sign in to comment.