Symfony 2.3: How do you configure SwiftMailer to automatically use a custom plugin?
Asked Answered
J

2

8

I have created a custom SwiftMailer plugin which I would like to have SwiftMailer use by default in my Symfony 2.3 application. The only documentation I can find in this regard is here: http://symfony.com/doc/current/reference/dic_tags.html#swiftmailer-plugin

I have set up the service as follows:

acme_test_bundle.swiftmailer.embed_images:
    class: Acme\TestBundle\SwiftMailer\Plugins\ImageEmbedPlugin
    tags:
        - { name: swiftmailer.plugin }

SwiftMailer is not using the plugin even though this service has been created. Have I done something wrong and is there anything else I should be doing?

Jessejessee answered 23/10, 2013 at 13:23 Comment(6)
Seems like you are doing it the right way. Is your plugin working when used outside symfony?Mangonel
When something strange happens first thing to do is: clear the cache. Did you do that? :)Vinyl
@Mangonel Yep it is working if I register it directly on the mailer objectJessejessee
@Vinyl Yep, I did clear the cache it still is not working.Jessejessee
Can you see your service doing php app/console container:debugVinyl
@Vinyl Yes it is appearing as an available service.Jessejessee
J
6

I managed to figure this out myself.

Contrary to what the Symfony 2.3 documentation says, you need to tag the service with which mailer you will be using (usually the 'default' one).

Therefore, I needed to change swiftmailer.plugin to swiftmailer.default.plugin

So, the service definition is now:

acme_test_bundle.swiftmailer.embed_images:
    class: Acme\TestBundle\SwiftMailer\Plugins\ImageEmbedPlugin
    tags:
        - { name: swiftmailer.default.plugin }
Jessejessee answered 29/10, 2013 at 18:23 Comment(0)
P
3

To provide some additional context to the OP's answer.

As of Symfony 2.3 in the SwiftmailerBundle CompilerPass process it performs the following.

$mailers = $container->getParameter('swiftmailer.mailers');
foreach ($mailers as $name => $mailer) {
      $plugins = $container->findTaggedServiceIds(sprintf('swiftmailer.%s.plugin', $name));
       foreach ($plugins as $id => $args) {
            $transport->addMethodCall('registerPlugin', [new Reference($id)]);
       }
}

Based on this you would need to add all of the mailer names to your tags that you would like to add the plugin to, in the format of swiftmailer.%mailer_name%.plugin. Replacing %mailer_name% with the name of your mailers.

When not using the multiple mailers configuration for swiftmailer the %mailer_name% is default which is set in the Bundle Configuration.

$v['default_mailer'] = isset($v['default_mailer']) ? (string) $v['default_mailer'] : 'default';
$v['mailers'] = array($v['default_mailer'] => $mailer);

Example config.yml

swiftmailer:
    default_mailer: first_mailer #alias: default
    mailers:
      first_mailer:
        #...
      second_mailer:
        #...


    services:
        #...
        swiftmailer.plugin.array_logger:
            class: Swift_Plugins_Loggers_ArrayLogger
        swiftmailer.plugin.logger:
            class: Swift_Plugins_LoggerPlugin
            arguments: ['@swiftmailer.plugin.array_logger']
            tags:
                - { name: swiftmailer.default.plugin }
                - { name: swiftmailer.second_mailer.plugin }
Palawan answered 29/7, 2016 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.