通过控制器中的内存发送电子邮件,并在 Symfony2 中的某些命令中假脱机

Send emails via memory in controllers and spool in some commands in Symfony2

我需要使用假脱机选项向我的用户发送大量电子邮件,但我不会将我的应用程序的整个配置更改为假脱机,因为我的注册系统向用户发送了一封电子邮件,我想要这封电子邮件即时发送。

有什么方法可以在不更改 swiftmailer 的全局配置的情况下做到这一点?

您可以配置不同的邮箱。例如:

swiftmailer:
    default_mailer: spool_mailer
    mailers:
        spool_mailer:
            spool:
                type: file
                path: /path/to/spool
            # ...
        instant_mailer:
            # ... 

然后根据您是否要假脱机使用一个或另一个电子邮件:

//in your controller
$spoolMailer = $this->get('swiftmailer.mailer.spool_mailer');
$spoolMailer->send(...);  //this will be spooled

$instantMailer = $this->get('swiftmailer.mailer.instant_mailer');
$instantMailer->send(...);  //this will be sent instantly