Skip to content

Commit

Permalink
Merge pull request #11 from stephane-fillion/master
Browse files Browse the repository at this point in the history
TYPO3 12 compatibility
  • Loading branch information
ameos authored Jan 22, 2024
2 parents 2252c77 + 46b00de commit 3c53488
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 241 deletions.
79 changes: 79 additions & 0 deletions Classes/EventListener/BeforeMailerSentMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Ameos\AmeosMailredirect\EventListener;

use Ameos\AmeosMailredirect\Service\RedirectService;
use Symfony\Component\Mime\Email;
use TYPO3\CMS\Core\Mail\Event\BeforeMailerSentMessageEvent;

class BeforeMailerSentMessage
{
/**
* @param RedirectService $redirectService
*/
public function __construct(private readonly RedirectService $redirectService)
{

}

/**
* event before mailer sent message
* redirect mail if redirection is enable
*
* @param BeforeMailerSentMessageEvent $event
* @return void
*/
public function __invoke(BeforeMailerSentMessageEvent $event): void
{
/** @var Email */
$message = $event->getMessage();
if (is_a($message, Email::class)) {
$prefixSubject = $this->redirectService->getSubjectPrefix();
if ($prefixSubject !== '') {
$message->subject($prefixSubject . ' ' . $message->getSubject());
}

if ($this->redirectService->isEnabled()) {
$originalRecipients = $this->redirectService->symfonyToAdress($message->getTo());
$message->to(...$this->redirectService->getRecipients());
$message->html(
sprintf(
'%s<br />To : %s',
$message->getHtmlBody(),
implode(';', $originalRecipients)
)
);
$message->text(
sprintf(
'%s%sTo : %s',
$message->getTextBody(),
chr(10),
implode(';', $originalRecipients)
)
);
}

if ($this->redirectService->isCopyEnabled()) {
$originalRecipients = $this->redirectService->symfonyToAdress($message->getCc());
$message->cc(...$this->redirectService->getRecipientsForCopy());
$message->html(
sprintf(
'%s<br />Cc : %s',
$message->getHtmlBody(),
implode(';', $originalRecipients)
)
);
$message->text(
sprintf(
'%s%sCc : %s',
$message->getTextBody(),
chr(10),
implode(';', $originalRecipients)
)
);
}
}
}
}
93 changes: 93 additions & 0 deletions Classes/Service/RedirectService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Ameos\AmeosMailredirect\Service;

use Symfony\Component\Mime\Address;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class RedirectService
{
/**
* @param ExtensionConfiguration $extensionConfiguration
*/
public function __construct(private readonly ExtensionConfiguration $extensionConfiguration)
{

}

/**
* return true if redirection is enable
*
* @return bool
*/
public function isEnabled(): bool
{
return (bool)$this->extensionConfiguration->get('ameos_mailredirect', 'activate') === true;
}

/**
* return true if copy redirection is enable
*
* @return bool
*/
public function isCopyEnabled(): bool
{
return (bool)$this->extensionConfiguration->get('ameos_mailredirect', 'copy/activate') === true;
}

/**
* return subject prefix
*
* @return string
*/
public function getSubjectPrefix(): string
{
return (string)$this->extensionConfiguration->get('ameos_mailredirect', 'subject_prefix');
}

/**
* Returns recipients
*
* @return array
*/
public function getRecipients(): array
{
return array_map(
fn ($address) => new Address($address),
GeneralUtility::trimExplode(
';',
$this->extensionConfiguration->get('ameos_mailredirect', 'recipient')
)
);
}

/**
* Returns recipients for copy
*
* @return array
*/
public function getRecipientsForCopy(): array
{
return array_map(
fn ($address) => new Address($address),
GeneralUtility::trimExplode(
';',
$this->extensionConfiguration->get('ameos_mailredirect', 'copy/recipient')
)
);
}

/**
* convert symfony address to raw text
*
* @param array $addresses
* @return array
*/
public function symfonyToAdress(array $addresses): array
{
return array_map(fn (Address $address) => $address->getAddress(), $addresses);
}
}
220 changes: 0 additions & 220 deletions Classes/Xclass/Mail/Mailer.php

This file was deleted.

Loading

0 comments on commit 3c53488

Please sign in to comment.