Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[SwiftmailerBundle] added support for the new memory spool

  • Loading branch information...
commit 9a0aefd1921351f6eb2655440900ec7345928b53 1 parent aa9b86e
@fabpot fabpot authored
View
2  src/Symfony/Bundle/SwiftmailerBundle/DependencyInjection/SwiftmailerExtension.php
@@ -87,6 +87,8 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('spool.xml');
if ($type === 'file') {
$loader->load('spool_file.xml');
+ } elseif ($type === 'memory') {
+ $loader->load('spool_memory.xml');
}
$container->setAlias('swiftmailer.transport.real', $transport);
$container->setAlias('swiftmailer.transport', 'swiftmailer.transport.spool');
View
54 src/Symfony/Bundle/SwiftmailerBundle/EventListener/EmailSenderListener.php
@@ -0,0 +1,54 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\SwiftmailerBundle\EventListener;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\HttpKernel\Event\PostResponseEvent;
+use Symfony\Component\HttpKernel\KernelEvents;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * Sends emails for the memory spool.
+ *
+ * Emails are sent on the kernel.terminate event.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class EmailSenderListener implements EventSubscriberInterface
+{
+ private $container;
+
+ public function __construct(ContainerInterface $container, $autoStart = false)
+ {
+ $this->container = $container;
+ }
+
+ public function onKernelTerminate(PostResponseEvent $event)
+ {
+ $transport = $this->container->get('mailer')->getTransport();
+ if (!$transport instanceof \Swift_Transport_SpoolTransport) {
+ return;
+ }
+
+ $spool = $transport->getSpool();
+ if (!$spool instanceof \Swift_MemorySpool) {
+ return;
+ }
+
+ $spool->flushQueue($this->container->get('swiftmailer.transport.real'));
@vicb
vicb added a note

this currently adds overhead for every requests as the transport always gets started, fix is in swiftmailer @ swiftmailer/swiftmailer#152

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ static public function getSubscribedEvents()
+ {
+ return array(KernelEvents::TERMINATE => 'onKernelTerminate');
+ }
+}
View
20 src/Symfony/Bundle/SwiftmailerBundle/Resources/config/spool_memory.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<container xmlns="http://symfony.com/schema/dic/services"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+ <parameters>
+ <parameter key="swiftmailer.spool.memory.class">Swift_MemorySpool</parameter>
+ <parameter key="swiftmailer.email_sender.listener.class">Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener</parameter>
+ </parameters>
+
+ <services>
+ <service id="swiftmailer.spool.memory" class="%swiftmailer.spool.memory.class%" public="false" />
+
+ <service id="swiftmailer.email_sender.listener" class="%swiftmailer.email_sender.listener.class%">
+ <tag name="kernel.event_subscriber" />
+ <argument type="service" id="service_container" />
+ </service>
+ </services>
+</container>

3 comments on commit 9a0aefd

@Seldaek
Collaborator

Only problem I see with this is that you can't notify the user anymore if email sending failed.

@pminnieur

You may store a flash in the session?

@vicb

this currently adds overhead for every requests as the transport always gets started, fix is in swiftmailer @ swiftmailer/swiftmailer#152

Please sign in to comment.
Something went wrong with that request. Please try again.