-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from stephane-fillion/master
TYPO3 12 compatibility
- Loading branch information
Showing
9 changed files
with
269 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.