From e5e0ba60b136f9a1b0d43b4a9b6644c9935fdc59 Mon Sep 17 00:00:00 2001 From: Vincent Lopes-Vicente Date: Fri, 2 Sep 2022 11:58:47 +0200 Subject: [PATCH] Fix for work with Symfony mailer --- Config/config.xml | 5 --- Config/module.xml | 2 +- EventListener/MailerListener.php | 76 +++++++++++++++----------------- TheliaMailCatcher.php | 13 ++++++ 4 files changed, 49 insertions(+), 47 deletions(-) diff --git a/Config/config.xml b/Config/config.xml index c0042c2..ea7c54c 100644 --- a/Config/config.xml +++ b/Config/config.xml @@ -4,9 +4,4 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd"> - - - - - diff --git a/Config/module.xml b/Config/module.xml index 948e794..a83b385 100644 --- a/Config/module.xml +++ b/Config/module.xml @@ -13,7 +13,7 @@ en_US fr_FR - 1.0.0 + 1.0.1 Gilles Bourgeat gilles.bourgeat@gmail.com diff --git a/EventListener/MailerListener.php b/EventListener/MailerListener.php index 2c1f877..b2c893f 100644 --- a/EventListener/MailerListener.php +++ b/EventListener/MailerListener.php @@ -12,11 +12,11 @@ namespace TheliaMailCatcher\EventListener; -use TheliaMailCatcher\Plugin\SwiftEventListenerPlugin; +use Symfony\Component\Mailer\Event\MessageEvent; +use Symfony\Component\Mailer\Header\MetadataHeader; +use Symfony\Component\Mime\Address; +use Symfony\Component\Mime\Email; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Thelia\Core\Event\MailTransporterEvent; -use Thelia\Core\Event\TheliaEvents; -use Thelia\Mailer\MailerFactory; use Thelia\Model\ConfigQuery; /** @@ -26,48 +26,42 @@ */ class MailerListener implements EventSubscriberInterface { - /** - * @param MailTransporterEvent $event - */ - public function addPlugin(MailTransporterEvent $event) + public function replaceRecipients(MessageEvent $event) { - if (!$event->hasTransporter()) { - $event->setMailerTransporter( - ConfigQuery::isSmtpEnable() ? $this->configureSmtp() : \Swift_MailTransport::newInstance() - ); + $message = $event->getMessage(); + if (!$message instanceof Email) { + return; } - /** @var MailerFactory $mailer */ - $event->getTransporter()->registerPlugin(new SwiftEventListenerPlugin()); - } - - /** - * @return \Swift_SmtpTransport - */ - protected function configureSmtp() - { - $smtpTransporter = \Swift_SmtpTransport::newInstance(ConfigQuery::getSmtpHost(), ConfigQuery::getSmtpPort()); + $emails = ConfigQuery::getNotificationEmailsList(); - if (ConfigQuery::getSmtpEncryption()) { - $smtpTransporter->setEncryption(ConfigQuery::getSmtpEncryption()); - } - if (ConfigQuery::getSmtpUsername()) { - $smtpTransporter->setUsername(ConfigQuery::getSmtpUsername()); - } - if (ConfigQuery::getSmtpPassword()) { - $smtpTransporter->setPassword(ConfigQuery::getSmtpPassword()); - } - if (ConfigQuery::getSmtpAuthMode()) { - $smtpTransporter->setAuthMode(ConfigQuery::getSmtpAuthMode()); - } - if (ConfigQuery::getSmtpTimeout()) { - $smtpTransporter->setTimeout(ConfigQuery::getSmtpTimeout()); - } - if (ConfigQuery::getSmtpSourceIp()) { - $smtpTransporter->setSourceIp(ConfigQuery::getSmtpSourceIp()); + if (!count($emails)) { + $emails = [ConfigQuery::getStoreEmail()]; } - return $smtpTransporter; + $message->getHeaders()->add( + new MetadataHeader( + "OriginalRecipient", + implode( + ",", + array_map( + function (Address $address) { + return $address->toString(); + }, + $message->getTo() + ) + ) + ) + ); + + $addresses = array_map(function ($email) {return new Address($email);}, $emails); + $event->getEnvelope() + ->setRecipients($addresses); + + $message->to($addresses[0]); + for ($i = 1; $i < count($addresses); $i++) { + $message->addTo($addresses[$i]); + } } /** @@ -76,7 +70,7 @@ protected function configureSmtp() public static function getSubscribedEvents() { return [ - TheliaEvents::MAILTRANSPORTER_CONFIG => ['addPlugin', 128] + MessageEvent::class => ['replaceRecipients', -600] ]; } } diff --git a/TheliaMailCatcher.php b/TheliaMailCatcher.php index a3d2726..04cbf10 100644 --- a/TheliaMailCatcher.php +++ b/TheliaMailCatcher.php @@ -12,6 +12,7 @@ namespace TheliaMailCatcher; +use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator; use Thelia\Module\BaseModule; /** @@ -23,4 +24,16 @@ class TheliaMailCatcher extends BaseModule { /** @var string */ const DOMAIN_NAME = 'theliamailcatcher'; + + /** + * Defines how services are loaded in your modules. + */ + public static function configureServices(ServicesConfigurator $servicesConfigurator): void + { + $servicesConfigurator->load(self::getModuleCode().'\\', __DIR__) + ->exclude([THELIA_MODULE_DIR.ucfirst(self::getModuleCode()).'/I18n/*']) + ->autowire(true) + ->autoconfigure(true); + } + }