8

我在 Symfony 2 中使用 SwiftMailer 包。我在 config.yml 文件中传递了我的 smtp 用户/密码设置,效果很好,但是当我发送邮件时,我需要从数据库中获取这些设置。我可以访问这个参数:

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

但是是否可以在运行时更改它们?我没有看到任何设置方法。非常感谢!

4

4 回答 4

6

非常感谢,但这不是我正在寻找的解决方案,根据内核请求,我不知道我将使用哪个帐户。我需要更改发送邮件循环中的设置。我找到了很酷的解决方案:

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 );    
}
于 2012-05-24T14:21:33.883 回答
3

您可以创建一个kernel.request事件侦听器,注入swiftmailer.transport.real并设置 smpt 信息,例如

监听类

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

}

服务减速,

your_listener:
    class: FQCN\Of\YourListener
    tags:
        - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
    arguments: [@swiftmailer.transport.real, @doctrine.orm.entity_manager]
于 2012-05-23T17:25:43.370 回答
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.

于 2013-07-12T00:11:51.890 回答
2

实际上,您应该调用$transport->stop(); ,因为在其他方式 Swift Mailer 不会重新连接,并且在 1 个脚本执行期间将使用旧设置

于 2013-12-28T18:38:36.513 回答