Last Updated: February 25, 2016
·
1.081K
· wireframe

Add Rails application name and environment to email subject

Configure Rails ActionMailer to automatically add the Rails application name to all delivered emails

For emails sent from non-production environments, It is also helpful to add the Rails environment to the subject line to make it easier to setup email rules/filters.

Example subject for production emails:
[MyApp] Forgot Password

Example subject for staging emails:
[MyApp STAGING] Forgot Password

config/initializers/add_appname_to_email_subject.rb

class AddAppnameToEmailSubject
  def self.delivering_email(mail)
    prefixes = []
    prefixes << Rails.application.class.parent_name
    prefixes << Rails.env.upcase unless Rails.env.production?
    prefix = "[#{prefixes.join(' ')}] "
    mail.subject.prepend(prefix)
  end
end
ActionMailer::Base.register_interceptor(AddAppnameToEmailSubject)