如何定义其他邮件服务以使用假脱机并在Symfony2中发送即时电子邮件

Dav*_*eto 7 dependency-injection spool swiftmailer symfony

在我的Symfony2网络应用程序中,我应该发送两种电子邮件:即时和批量.应立即发送即时电子邮件,同时应使用假脱机发送批量电子邮件.使用Symfony2中Swiftmailer的默认配置是不可能的,因为只有一个邮件服务.

类似的问题已经在SO(如何假脱机电子邮件(在一个任务中)和在其他控制器中发送正常的电子邮件?)中提出了没有运气,但根据这个github线程(https://github.com/) symfony/SwiftmailerBundle/issues/6)可以创建第二个邮件服务,可以配置完全不同于默认邮件服务.有人(stof)建议作为一种可能的解决方案,遵循SwiftmailerBundle(https://github.com/symfony/SwiftmailerBundle/blob/master/Resources/config/swiftmailer.xml)中的配置来创建这项新服务,但是我不知道究竟是怎么做到的.

有没有人知道如何创建一个额外的邮件服务,我可以将其配置为假脱机,同时使用默认邮件服务发送常规(即时)电子邮件?

Dav*_*eto 12

我在这里找到了解决方案

这是我实现它的方式:

首先,我将默认邮件程序服务配置为用作发送批量电子邮件的假脱机.

(config.yml)

swiftmailer:
    transport: %mailer_transport%
    encryption: %mailer_encryption%
    auth_mode: %mailer_auth_mode%
    host: %mailer_host%
    username: %mailer_user%
    password: %mailer_password%
    spool:
        type: file
        path: "%kernel.root_dir%/spool"
Run Code Online (Sandbox Code Playgroud)

然后,在我的一个捆绑包(CommonBundle)中,我注册了一个名为"instant_mailer"的新服务,该服务映射到Swiftmailer类.

(service.yml)

instant_mailer:
    class: %swiftmailer.class%
    arguments: ["@?swiftmailer.transport.real"]
Run Code Online (Sandbox Code Playgroud)

最后,在我的控制器中,无论何时我想发送电子邮件,我都会这样做:

$mailer = $this->get('mailer');
Run Code Online (Sandbox Code Playgroud)

并发送即时电子邮件:

$mailer = $this->get('instant_mailer');
Run Code Online (Sandbox Code Playgroud)

  • 太好了!但是,如果一个环境使用假脱机而另一个环境不使用假脱机,则可能会遇到问题.您可能想要在切换到.real传输之前检查`$ this-> get('mailer') - > getTransport()instanceof\Swift_Transport_SpoolTransport`,因为它可能不存在! (2认同)