在我的Symfony2网络应用程序中,我应该发送两种电子邮件:即时和批量。即时电子邮件应立即发送,而批量电子邮件应使用假脱机发送。使用Symfony2中Swiftmailer的默认配置,不可能做到这一点,因为只有一个mailer服务。
SO中也提出了类似的问题(如何(在任务中)假脱机电子邮件并在其他控制器中发送正常电子邮件?)运气不好,但根据这个github线程(https://github.com/symfony/SwiftmailerBundle/issues/6)可以创建第二个邮件服务,该服务可以被配置为与默认服务完全不同。那里有人(stof)推荐了一个可能的解决方案来遵循SwiftmailerBundle中的配置(https://github.com/symfony/SwiftmailerBundle/blob/master/Resources/config/swiftmailer.xml)创建这个新服务,但我不知道具体是如何做到的。
有人知道如何创建一个额外的邮件服务,我可以将其配置为假脱机,同时使用默认的邮件服务发送定期(即时)电子邮件吗?
我在这里找到了解决方案
这就是我实现它的方式:
首先,我将默认的邮件服务配置为作为假脱机发送批量电子邮件。
(配置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"
然后,在我的一个bundle(CommonBundle)中,我注册了一个名为"instant_iler"的新服务,该服务映射到Swiftmailer类。
(service.yml)
instant_mailer:
class: %swiftmailer.class%
arguments: ["@?swiftmailer.transport.real"]
最后,在我的控制器中,每当我想通过假脱机发送电子邮件时,我只需执行以下操作:
$mailer = $this->get('mailer');
发送即时电子邮件:
$mailer = $this->get('instant_mailer');