Reputation: 4853
I would like all the emails sent from our staging server to have the phrase "[STAGING] " prefaced in the subject. Is there an elegant way to do this in Rails 3.2 using ActionMailer?
Upvotes: 6
Views: 1894
Reputation: 135
This works for Rails 4.x
class UserMailer < ActionMailer::Base
after_action do
mail.subject.prepend('[Staging] ') if Rails.env.staging?
end
(...)
end
Upvotes: 5
Reputation: 4853
Here's an elegant solution I found using ActionMailer Interceptor based on 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
Upvotes: 19
Reputation: 1648
Not really, inheritance is about as elegant as it gets.
class OurNewMailer < ActionMailer::Base
default :from => '[email protected]',
:return_path => '[email protected]'
def subjectify subject
return "[STAGING] #{subject}" if Rails.env.staging?
subject
end
end
Then you can inherit from each of your mailers.
# 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
I don't think this is as clean as what you were hopeing for, but this will dry it up a bit.
Upvotes: 0