我正在使用Rails 4.2,希望覆蓋特定環境下所有ActionMailer郵件程序的to
字段。在這種情況下,我想覆蓋在分段中使用的所有郵件程序的「到」字段。我的目標是讓分段環境以與生產完全相同的方式傳遞郵件,但將其全部轉儲到測試收件箱中。根據環境重寫到ActionMailer中的字段
我知道有些服務可以幫助解決這個問題,但我的目標是使用我的生產API作爲徹底的測試來分期交付。
我希望在郵件發射之前我可以使用mixin或其他東西來重置to
字段。
我正在使用Rails 4.2,希望覆蓋特定環境下所有ActionMailer郵件程序的to
字段。在這種情況下,我想覆蓋在分段中使用的所有郵件程序的「到」字段。我的目標是讓分段環境以與生產完全相同的方式傳遞郵件,但將其全部轉儲到測試收件箱中。根據環境重寫到ActionMailer中的字段
我知道有些服務可以幫助解決這個問題,但我的目標是使用我的生產API作爲徹底的測試來分期交付。
我希望在郵件發射之前我可以使用mixin或其他東西來重置to
字段。
不知道的Rails的是什麼版本,正在使用,但您可能會考慮使用新的郵件攔截器來完成此操作。
主要優點是它不會直接混淆ActionMailer類。
http://guides.rubyonrails.org/action_mailer_basics.html#intercepting-emails
複製他們的榜樣:
class SandboxEmailInterceptor
def self.delivering_email(message)
message.to = ['[email protected]']
end
end
配置/初始化/ sandbox_email_interceptor.rb:
ActionMailer::Base.register_interceptor(SandboxEmailInterceptor) if Rails.env.staging?
最簡單的方法是檢查哪個環境正在運行,並相應地設置to
字段。例如,一個簡單的密碼重置郵件可能看起來像:
class UserMailer < ActionMailer::Base
default from: "[email protected]"
def reset_password(user_id)
@user = User.find(user_id)
@url = reset_password_users_url(token: @user.password_reset_token)
mail(to: @user.email, subject: '[Example] Please reset your password')
end
end
我們檢查臨時環境和路由所有這些郵件到[email protected]
:
class UserMailer < ActionMailer::Base
default from: "[email protected]"
def reset_password(user_id)
@user = User.find(user_id)
@url = reset_password_users_url(token: @user.password_reset_token)
to = Rails.env.staging? ? '[email protected]' : @user.email
mail(to: to, subject: '[Example] Please reset your password')
end
end
我用Rails 4.2,這正是我要找的! – 2015-02-07 00:40:18