Changing smtp settings in SwiftMailer dynamically

半城伤御伤魂 提交于 2019-11-29 13:34:27

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 );    
}

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]

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.

Andrew Zhilin

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!