Skip to content

Commit

Permalink
Fix for work with Symfony mailer
Browse files Browse the repository at this point in the history
  • Loading branch information
lopes-vincent committed Sep 2, 2022
1 parent f657433 commit e5e0ba6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 47 deletions.
5 changes: 0 additions & 5 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<services>
<service id="thelia.mail.catcher.listener" class="TheliaMailCatcher\EventListener\MailerListener">
<tag name="kernel.event_subscriber"/>
</service>
</services>
</config>
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.0</version>
<version>1.0.1</version>
<author>
<name>Gilles Bourgeat</name>
<email>[email protected]</email>
Expand Down
76 changes: 35 additions & 41 deletions EventListener/MailerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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]);
}
}

/**
Expand All @@ -76,7 +70,7 @@ protected function configureSmtp()
public static function getSubscribedEvents()
{
return [
TheliaEvents::MAILTRANSPORTER_CONFIG => ['addPlugin', 128]
MessageEvent::class => ['replaceRecipients', -600]
];
}
}
13 changes: 13 additions & 0 deletions TheliaMailCatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace TheliaMailCatcher;

use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Thelia\Module\BaseModule;

/**
Expand All @@ -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);
}

}

0 comments on commit e5e0ba6

Please sign in to comment.