2012-09-26 25 views
0

我將ActionMailer配置爲通過開發模式下的Gmail發送電子郵件。如何配置ActionMailer以開發模式有條件地發送電子郵件?

配置/ development.rb

config.action_mailer.default_url_options = { host: ENV["MY_DOMAIN"] } 
config.action_mailer.raise_delivery_errors = true 
config.action_mailer.delivery_method = :smtp 
config.action_mailer.perform_deliveries = true 
config.action_mailer.smtp_settings = { 
    address: "smtp.gmail.com", 
    port: 587, 
    domain: ENV["MY_DOMAIN"], 
    authentication: "plain", 
    enable_starttls_auto: true, 
    user_name: ENV["MY_USERNAME"], 
    password: ENV["MY_PASSWORD"] 
} 

我有這樣的設置,使我的設計人員可以觸發併發送測試HTML郵件到指定地址進行測試在各種瀏覽器/設備。

但是,在開發模式下,我想阻止所有未發送到指定電子郵件地址的傳出電子郵件。

我在尋找類似:

config.action_mailer.perform_deliveries = target_is_designated_email_address? 

...但我需要好歹來檢查郵件的實例,以確保它被髮送到正確的地址。

有什麼想法?

謝謝!

回答

2

查看ActionMailer使用的郵件gem中的郵件攔截器。你可以在railscast on ActionMailer找到更多關於他們的信息。

直接來自railscast的相關部分:

創建一個類來重定向郵件。

class DevelopmentMailInterceptor 
    def self.delivering_email(message) 
    message.subject = "[#{message.to}] #{message.subject}" 
    message.to = "[email protected]" 
    end 
end 

註冊只發展方式攔截:

Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development? 

那麼它只會替換「[[email protected]一些主題」的主題行和消息重定向到你想要的任何人。

+0

完美。非常感謝 – doublea

相關問題