소스 검색

[SwiftmailerBundle] Added ImpersonateSenderPlugin

arjen 14 년 전
부모
커밋
ed4f659693

+ 1 - 0
src/Symfony/Bundle/SwiftmailerBundle/DependencyInjection/Configuration.php

@@ -80,6 +80,7 @@ class Configuration implements ConfigurationInterface
                         ->scalarNode('path')->defaultValue('%kernel.cache_dir%/swiftmailer/spool')->end()
                     ->end()
                 ->end()
+                ->scalarNode('sender_address')->end()
                 ->scalarNode('delivery_address')->end()
                 ->booleanNode('disable_delivery')->end()
                 ->booleanNode('logging')->defaultValue($this->debug)->end()

+ 7 - 0
src/Symfony/Bundle/SwiftmailerBundle/DependencyInjection/SwiftmailerExtension.php

@@ -95,6 +95,13 @@ class SwiftmailerExtension extends Extension
             $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.messagelogger')));
             $container->findDefinition('swiftmailer.data_collector')->addTag('data_collector', array('template' => 'SwiftmailerBundle:Collector:swiftmailer', 'id' => 'swiftmailer'));
         }
+        
+        if (isset($config['sender_address']) && $config['sender_address']) {
+            $container->setParameter('swiftmailer.sender_address', $config['sender_address']);
+            $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.impersonate')));
+        } else {
+            $container->setParameter('swiftmailer.sender_address', null);
+        }
 
         if (isset($config['delivery_address']) && $config['delivery_address']) {
             $container->setParameter('swiftmailer.single_address', $config['delivery_address']);

+ 47 - 0
src/Symfony/Bundle/SwiftmailerBundle/Plugin/ImpersonateSenderPlugin.php

@@ -0,0 +1,47 @@
+<?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\Plugin;
+
+/**
+ * Replaces the sender of a message.
+ *
+ * @author Arjen Brouwer <info@arjenbrouwer.nl>
+ */
+class ImpersonateSenderPlugin implements \Swift_Events_SendListener {
+
+    private $_sender;
+
+    public function __construct($sender) {
+        $this->_sender = $sender;
+    }
+
+    public function beforeSendPerformed(\Swift_Events_SendEvent $evt) {
+        $message = $evt->getMessage();
+        $headers = $message->getHeaders();
+
+        // save current recipients
+        $headers->addPathHeader('X-Swift-Return-Path', $message->getReturnPath());
+
+        // replace them with the one to send to
+        $message->setReturnPath($this->_sender);
+    }
+
+    public function sendPerformed(\Swift_Events_SendEvent $evt) {
+        $message = $evt->getMessage();
+
+        // restore original headers
+        $headers = $message->getHeaders();
+        
+        if ($headers->has('X-Swift-Return-Path')) {
+            $message->setReturnPath($headers->get('X-Swift-Return-Path')->getAddress());
+            $headers->removeAll('X-Swift-Return-Path');
+        }
+    }
+}

+ 5 - 0
src/Symfony/Bundle/SwiftmailerBundle/Resources/config/swiftmailer.xml

@@ -15,6 +15,7 @@
         <parameter key="swiftmailer.plugin.redirecting.class">Swift_Plugins_RedirectingPlugin</parameter>
         <parameter key="swiftmailer.plugin.blackhole.class">Swift_Plugins_BlackholePlugin</parameter>
         <parameter key="swiftmailer.plugin.messagelogger.class">Symfony\Bundle\SwiftmailerBundle\Logger\MessageLogger</parameter>
+        <parameter key="swiftmailer.plugin.impersonate.class">Symfony\Bundle\SwiftmailerBundle\Plugin\ImpersonateSenderPlugin</parameter>
         <parameter key="swiftmailer.data_collector.class">Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector</parameter>
     </parameters>
 
@@ -63,6 +64,10 @@
 
         <service id="swiftmailer.plugin.blackhole" class="%swiftmailer.plugin.blackhole.class%" public="false" />
 
+        <service id="swiftmailer.plugin.impersonate" class="%swiftmailer.plugin.impersonate.class%" public="false">
+            <argument>%swiftmailer.sender_address%</argument>
+        </service>
+             
         <service id="swiftmailer.plugin.messagelogger" class="%swiftmailer.plugin.messagelogger.class%" />
 
         <service id="swiftmailer.data_collector" class="%swiftmailer.data_collector.class%" public="false">