Changing smtp settings in SwiftMailer dynamically

I use SwiftMailer package with Symfony 2. I pass my smtp user / password settings in config.yml file, it works fine, but I need to accept these settings from the database when I send mail. I can use these options:

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

But can they be changed at runtime? I do not see any setter methods. many thanks!

+7
source share
4 answers

Thank you very much, but this is not the solution I was looking for at the request of the kernel. I do not know which account to use. I needed to change the settings inside my mail loop. I found a 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 ); } 
+6
source

You can create a kernel.request event kernel.request , type swiftmailer.transport.real and set smpt info eg

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 Announcement,

 your_listener: class: FQCN\Of\YourListener tags: - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } arguments: [@swiftmailer.transport.real, @doctrine.orm.entity_manager] 
+3
source

I know this is a little outdated, but I wanted to get an answer if it helps someone else. We use a message queue manager with SMTP transport and must have configured SMTP server connections depending on the site.

Our solution was to modify the Swiftmailer to provide additional data for each message, as well as bind it to the Symfony2 Event Dispatcher. This allowed us to extract connection information from each message while flushing the coil.

We turned it into a bundle so that others could use it. You can read about it here .

+2
source

In fact, you should call $transport->stop(); because otherwise Swift Mailer will not reconnect and the old configuration will be used at runtime

+2
source

All Articles