-
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.
[FEATURE] Add support for lux_letter mail sending redirection
- Loading branch information
Luc MULLER
committed
Mar 19, 2024
1 parent
3c53488
commit 451aaab
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
Classes/EventListener/LuxSendMailSendNewsletterBeforeMailMessageEvent.php
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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ameos\AmeosMailredirect\EventListener; | ||
|
||
use Ameos\AmeosMailredirect\Service\RedirectService; | ||
use In2code\Luxletter\Events\SendMailSendNewsletterBeforeMailMessageEvent; | ||
use In2code\Luxletter\Mail\MailMessage; | ||
|
||
class LuxSendMailSendNewsletterBeforeMailMessageEvent | ||
{ | ||
/** | ||
* @param RedirectService $redirectService | ||
*/ | ||
public function __construct(private readonly RedirectService $redirectService) | ||
{ | ||
} | ||
|
||
/** | ||
* event before mailer sent message | ||
* redirect mail if redirection is enable | ||
* | ||
* @param SendMailSendNewsletterBeforeEvent $event | ||
* @return void | ||
*/ | ||
public function __invoke(SendMailSendNewsletterBeforeMailMessageEvent $event): void | ||
{ | ||
/** @var MailMessage */ | ||
$message = $event->getMailMessage(); | ||
if (is_a($message, MailMessage::class)) { | ||
$prefixSubject = $this->redirectService->getSubjectPrefix(); | ||
|
||
if ($prefixSubject !== '') { | ||
$message->setSubject($prefixSubject . ' ' . $message->getSubject()); | ||
} | ||
|
||
if ($this->redirectService->isEnabled()) { | ||
$originalRecipients = array_keys($event->getReceiver()); | ||
$originalRecipientsCopy = $this->redirectService->symfonyToAdress($message->getCc()); | ||
|
||
$receiver = $this->redirectService->getRecipientsRaw(); | ||
$message->setTo($receiver); | ||
|
||
$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()) { | ||
$message->html( | ||
sprintf( | ||
'%s<br />Cc : %s', | ||
$message->getHtmlBody(), | ||
implode(';', $originalRecipientsCopy) | ||
) | ||
); | ||
$message->text( | ||
sprintf( | ||
'%s%sCc : %s', | ||
$message->getTextBody(), | ||
chr(10), | ||
implode(';', $originalRecipientsCopy) | ||
) | ||
); | ||
$message->cc(...$this->redirectService->getRecipientsForCopy()); | ||
} | ||
} | ||
} | ||
} |
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
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