Skip to content
This repository
Browse code

[SwiftmailerBundle] added support for the new memory spool

  • Loading branch information...
commit 9a0aefd1921351f6eb2655440900ec7345928b53 1 parent aa9b86e
Fabien Potencier authored December 16, 2011
2  src/Symfony/Bundle/SwiftmailerBundle/DependencyInjection/SwiftmailerExtension.php
@@ -87,6 +87,8 @@ public function load(array $configs, ContainerBuilder $container)
87 87
             $loader->load('spool.xml');
88 88
             if ($type === 'file') {
89 89
                 $loader->load('spool_file.xml');
  90
+            } elseif ($type === 'memory') {
  91
+                $loader->load('spool_memory.xml');
90 92
             }
91 93
             $container->setAlias('swiftmailer.transport.real', $transport);
92 94
             $container->setAlias('swiftmailer.transport', 'swiftmailer.transport.spool');
54  src/Symfony/Bundle/SwiftmailerBundle/EventListener/EmailSenderListener.php
... ...
@@ -0,0 +1,54 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * This file is part of the Symfony package.
  5
+ *
  6
+ * (c) Fabien Potencier <fabien@symfony.com>
  7
+ *
  8
+ * For the full copyright and license information, please view the LICENSE
  9
+ * file that was distributed with this source code.
  10
+ */
  11
+
  12
+namespace Symfony\Bundle\SwiftmailerBundle\EventListener;
  13
+
  14
+use Symfony\Component\DependencyInjection\ContainerInterface;
  15
+use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  16
+use Symfony\Component\HttpKernel\KernelEvents;
  17
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18
+
  19
+/**
  20
+ * Sends emails for the memory spool.
  21
+ *
  22
+ * Emails are sent on the kernel.terminate event.
  23
+ *
  24
+ * @author Fabien Potencier <fabien@symfony.com>
  25
+ */
  26
+class EmailSenderListener implements EventSubscriberInterface
  27
+{
  28
+    private $container;
  29
+
  30
+    public function __construct(ContainerInterface $container, $autoStart = false)
  31
+    {
  32
+        $this->container = $container;
  33
+    }
  34
+
  35
+    public function onKernelTerminate(PostResponseEvent $event)
  36
+    {
  37
+        $transport = $this->container->get('mailer')->getTransport();
  38
+        if (!$transport instanceof \Swift_Transport_SpoolTransport) {
  39
+            return;
  40
+        }
  41
+
  42
+        $spool = $transport->getSpool();
  43
+        if (!$spool instanceof \Swift_MemorySpool) {
  44
+            return;
  45
+        }
  46
+
  47
+        $spool->flushQueue($this->container->get('swiftmailer.transport.real'));
  48
+    }
  49
+
  50
+    static public function getSubscribedEvents()
  51
+    {
  52
+        return array(KernelEvents::TERMINATE => 'onKernelTerminate');
  53
+    }
  54
+}
20  src/Symfony/Bundle/SwiftmailerBundle/Resources/config/spool_memory.xml
... ...
@@ -0,0 +1,20 @@
  1
+<?xml version="1.0" encoding="UTF-8" ?>
  2
+
  3
+<container xmlns="http://symfony.com/schema/dic/services"
  4
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5
+    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
  6
+
  7
+    <parameters>
  8
+        <parameter key="swiftmailer.spool.memory.class">Swift_MemorySpool</parameter>
  9
+        <parameter key="swiftmailer.email_sender.listener.class">Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener</parameter>
  10
+    </parameters>
  11
+
  12
+    <services>
  13
+        <service id="swiftmailer.spool.memory" class="%swiftmailer.spool.memory.class%" public="false" />
  14
+
  15
+        <service id="swiftmailer.email_sender.listener" class="%swiftmailer.email_sender.listener.class%">
  16
+            <tag name="kernel.event_subscriber" />
  17
+            <argument type="service" id="service_container" />
  18
+        </service>
  19
+    </services>
  20
+</container>

3 notes on commit 9a0aefd

Jordi Boggiano
Collaborator

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

Pierre Minnieur

You may store a flash in the session?

Victor Berchet

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.