diff --git a/src/DependencyInjection/SetonoSyliusMailerExtension.php b/src/DependencyInjection/SetonoSyliusMailerExtension.php index 5658e03..c0c4837 100644 --- a/src/DependencyInjection/SetonoSyliusMailerExtension.php +++ b/src/DependencyInjection/SetonoSyliusMailerExtension.php @@ -32,8 +32,12 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load('services.xml'); - if (!ContainerBuilder::willBeAvailable('symfony/mailer', MailerInterface::class, ['symfony/framework-bundle'])) { + if (ContainerBuilder::willBeAvailable('symfony/mailer', MailerInterface::class, ['symfony/framework-bundle'])) { $loader->load('services/conditional/symfony_mailer.xml'); } + + if (ContainerBuilder::willBeAvailable('swiftmailer/swiftmailer', \Swift_Mailer::class, ['symfony/swiftmailer-bundle'])) { + $loader->load('services/conditional/swiftmailer.xml'); + } } } diff --git a/src/EventSubscriber/SwiftMailerLoggerSubscriber.php b/src/EventSubscriber/SwiftMailerLoggerSubscriber.php new file mode 100644 index 0000000..40a0588 --- /dev/null +++ b/src/EventSubscriber/SwiftMailerLoggerSubscriber.php @@ -0,0 +1,57 @@ +managerRegistry = $managerRegistry; + } + + public static function getSubscribedEvents(): array + { + return [ + 'sendPerformed' => 'log', + ]; + } + + public function log(\Swift_Events_SendEvent $event): void + { + $message = $event->getMessage(); + + /** @var SentEmailInterface $sentEmail */ + $sentEmail = $this->emailFactory->createNew(); + + $sentEmail->setSubject($message->getSubject()); + $sentEmail->setHtmlBody($message->getBody()); + + /** @psalm-suppress MixedArgumentTypeCoercion */ + $sentEmail->setTo(array_keys($message->getTo())); + + /** @psalm-suppress MixedArgumentTypeCoercion */ + $sentEmail->setReplyTo(array_keys($message->getReplyTo())); + + /** @psalm-suppress MixedArgumentTypeCoercion */ + $sentEmail->setCc(array_keys($message->getCc())); + + /** @psalm-suppress MixedArgumentTypeCoercion */ + $sentEmail->setBcc(array_keys($message->getBcc())); + + $manager = $this->getManager($sentEmail); + $manager->persist($sentEmail); + $manager->flush(); + } +} diff --git a/src/Resources/config/services/conditional/swiftmailer.xml b/src/Resources/config/services/conditional/swiftmailer.xml new file mode 100644 index 0000000..23bd6e6 --- /dev/null +++ b/src/Resources/config/services/conditional/swiftmailer.xml @@ -0,0 +1,12 @@ + + + + + + + + + + +