Changing smtp settings in SwiftMailer dynamically
Asked Answered
M

4

9

I'm using SwiftMailer bundle with Symfony 2. I pass my smtp user/password settings in config.yml file, it works great, but I need to take this settings from database, when I'm sending mail. I can acces this params:

$mailer = $this->getContainer()->get('mailer')->getTransport();

But is it possible to change them on runtime ? I dont see any setter methods. many thanks!

Mustang answered 23/5, 2012 at 15:26 Comment(3)
I'm just curious, how do you store the password?Fairfax
@Fairfax I see what you did there. Yes, this question must be answered!Whereof
@Whereof haha that old comment of mine :PFairfax
M
6

Many thanks, but it's not the solution i was looking, on kernel request I don't know which account I'll use. I needed to change settings inside my send mail loop. I found pretty cool solution:

foreach ($locations as $location) {
    // get settings for account
    $user = $location->getSmtpUser();
    $pass = $location->getSmtpPass();

    // switch to new settings
    $transport = $this->getContainer()->get('mailer')->getTransport();            
    $ext = $transport->getExtensionHandlers();
    $auth_handler = $ext[0];            
    $auth_handler->setUserName($user);
    $auth_handler->setPassword($pass);

    // send message using new settings
    $message = \Swift_Message::newInstance()
         ->setSubject( $subject )
         ->setFrom( $from )
         ->setTo( $email )
         ->setBody( $body )
         ->setContentType('text/html');

       $this->getContainer()->get('mailer')->send( $message );    
}
Mustang answered 24/5, 2012 at 14:21 Comment(0)
C
3

You can create a kernel.request event listener, inject swiftmailer.transport.real and set smpt info e.g

Listener class

namespace Namespace\Of\YourListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class YourListener implements EventSubscriberInterface
{

    /**
     * @var Swift_Transport_EsmtpTransport
     */
    private $transport;

    /**
     * @var Doctrine\ORM\EntityManager
     */
    private $em;

    public function __construct($transport, $em)
    {
        $this->transport = $transport;
        $this->em = $em;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        //fetch info from db
        $this->transport->setHost("host");
        $this->transport->setPort("port");
        $this->transport->setUserName("username");
        $this->transport->setPassword("pass");
    }

    static public function getSubscribedEvents()
    {
        return array(
            KernelEvents::REQUEST => array('onKernelRequest', 0)
        );
    }

}

Service decleration,

your_listener:
    class: FQCN\Of\YourListener
    tags:
        - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
    arguments: [@swiftmailer.transport.real, @doctrine.orm.entity_manager]
Carillon answered 23/5, 2012 at 17:25 Comment(0)
D
2

I know this is a bit old, but I wanted to get an answer in in-case it helps somebody else. We are using the file spooler with an SMTP transport and needed to have customized SMTP server connections depending on site.

Our solution was to modify Swiftmailer to allow for some additional data on each message as well as tying it into Symfony2's Event Dispatcher. This allowed us to extract the connection info from each message at the time of the spool flushing.

We made it into a bundle so it can be leveraged by others. You can read about it here.

Dodgem answered 12/7, 2013 at 0:11 Comment(0)
K
2

In fact, you should call $transport->stop(); because in other way Swift Mailer will not reconnect and old setting will be used during 1 script execution

Kulseth answered 28/12, 2013 at 18:38 Comment(3)
Hi which answer are you referring to? Thanks!Elyseelysee
any from listed above. If you change Swift settings dynamically - you should call stop after it to apply changes :)Kulseth
You saved a lot of time for me buddy :) Thank you very much! I tried to send emails in a loop with different auth settings and was getting the error Expected response code 250 but got code 553 all the time.Cancroid

© 2022 - 2024 — McMap. All rights reserved.