Symfony2 different configuration in different bundle?

I have this configuration in config.yml for Swiftmailer:

swiftmailer:
    transport: gmail
    username: [email protected]
    password: xxx
    delivery_address: [email protected]
    spool:
        type: file
        path: %kernel.cache_dir%/swiftmailer/spool
    antiflood:
        threshold:            99
        sleep:                0

But I need have one configuration to one bundle and another configuration to another bundle.

How I can do it?

Upvotes: 0

Views: 545

Answers (1)

Vitalii Zurian
Vitalii Zurian

Reputation: 17976

Well... You actually can get the mailer service inside your bundles and configure them in a way you need. Just get transport instance, configure its handlers and create new mailer instance with injecting configured transport there:

    $transport = $this->get('swiftmailer.transport');
    $transport->setHost('smtp.gmail.com');
    $transport->setEncryption('ssl');

    $handlers = $transport->getExtensionHandlers();

    $handler = $handlers[0];
    $handler->setUsername('');
    $handler->setPassword('');
    $handler->setAuthMode('login');

    $mailer = \Swift_Mailer::newInstance($transport);

I've set some properties above assuming that you want to use gmail transport. In vendor/symfony/swiftmailer-bundle/Symfony/Bundle/SwiftmailerBundle/DependencyInjection/SwiftmailerExtension.php there is simple check for this transport:

    //...
    } elseif ('gmail' === $config['transport']) {
        $config['encryption'] = 'ssl';
        $config['auth_mode'] = 'login';
        $config['host'] = 'smtp.gmail.com';
        $transport = 'smtp';
    } else {
    //...

You can try to configure spool by getting its container (you have to do it before getting mailer service):

$this->getContainer()
    ->setParameter('swiftmailer.spool.file.path, '%kernel.cache_dir%/swiftmailer/spool');

But this filepath should be used by default. You just have to enable spooling:

$this->getContainer()->setParameter('swiftmailer.spool.enabled', true);

antiflood can be configured in the similar way:

$this->getContainer()->setParameter('swiftmailer.plugin.antiflood.threshold', 99);
$this->getContainer()->setParameter('swiftmailer.plugin.antiflood.sleep', 0);

Hope it helps

Upvotes: 1

Related Questions