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

时间:2012-09-04 19:33:50

标签: symfony dependency-injection swiftmailer spool

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

此处已经在SO(How to spool emails (in a task) and send normal emails in the moment in the other controllers?)中提出了类似的问题而没有运气,但是根据这个github线程(https://github.com/symfony/SwiftmailerBundle/issues/6),可以创建一个可以完全不同的第二个邮件服务比默认的。有人(stof)建议作为一个可能的解决方案来遵循SwiftmailerBundle(https://github.com/symfony/SwiftmailerBundle/blob/master/Resources/config/swiftmailer.xml)中的配置来创建这个新服务,但我不知道究竟是怎么做的。

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

1 个答案:

答案 0 :(得分:12)

我找到了解决方案here

这是我实施它的方式:

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

(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"

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

(service.yml)

instant_mailer:
    class: %swiftmailer.class%
    arguments: ["@?swiftmailer.transport.real"]

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

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

发送即时电子邮件:

$mailer = $this->get('instant_mailer');