我希望我们的登台服务器发送的所有电子邮件都在主题中以“[STAGING]”开头。使用ActionMailer在Rails 3.2中有一种优雅的方法吗?
答案 0 :(得分:18)
以下是我根据ActionMailer Interceptor使用an existing answer找到的优雅解决方案。
# config/initializers/change_staging_email_subject.rb
if Rails.env.staging?
class ChangeStagingEmailSubject
def self.delivering_email(mail)
mail.subject = "[STAGING] " + mail.subject
end
end
ActionMailer::Base.register_interceptor(ChangeStagingEmailSubject)
end
答案 1 :(得分:2)
这适用于Rails 4.x
class UserMailer < ActionMailer::Base
after_action do
mail.subject.prepend('[Staging] ') if Rails.env.staging?
end
(...)
end
答案 2 :(得分:0)
实际上,继承并不像它那样优雅。
class OurNewMailer < ActionMailer::Base
default :from => 'no-reply@example.com',
:return_path => 'system@example.com'
def subjectify subject
return "[STAGING] #{subject}" if Rails.env.staging?
subject
end
end
然后你可以从你的每个邮件继承。
# modified from the rails guides
class Notifier < OurNewMailer
def welcome(recipient)
@account = recipient
mail(:to => recipient.email_address_with_name, :subject => subjectify("Important Message"))
end
end
我认为这不像你所希望的那样干净,但这会使它干涸。