2012-09-04 13 views
6

在我的Symfony2 Web應用程序中,我應該發送兩種類型的電子郵件:即時消息和批量消息。應立即發送即時電子郵件,而應使用線軸發送大量電子郵件。使用Symfony2中的Swiftmailer的默認配置是不可能的,因爲只有一個郵件服務。如何在Symfony2中定義一個額外的郵件服務來使用假脫機程序併發送即時電子郵件

類似的問題已經被問這裏的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)中的配置來創建此新服務,但我不知道具體做了什麼。

有誰知道如何創建一個額外的郵件服務,我可以配置爲假脫機,同時默認的郵件服務發送常規(即時)的電子郵件?

回答

11

我找到了解決辦法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"] 

最後,在我的控制器,每當我想發送一封電子郵件usning閥芯我只是做:

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

併發送一封即時郵件:

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

不錯!但是如果一個環境使用假脫機而另一個環境不使用,則可能會遇到問題。在切換到.real傳輸之前,您可能需要檢查'$ this-> get('mailer') - > getTransport()instanceof \ Swift_Transport_SpoolTransport',因爲它可能不存在! – bksunday

相關問題