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

Prefix all emails with application name and Rails ENV

Add prefix to all delivered emails containing application name and Rails environment which is helpful for setting up email filters for non-production emails.

Examples:

[MyApp] Forgot Password
[MyApp STAGING] Forgot Password
# config/initializers/add_appname_to_email_subject.rb
class AddAppnameToEmailSubject
  COMPANY_NAME = 'MyApp'

  def self.delivering_email(mail)
    mail.subject.prepend(email_prefix)
  end

  def self.email_prefix
    prefixes = []
    prefixes << COMPANY_NAME
    prefixes << Rails.env.upcase unless Rails.env.production?
    "[#{prefixes.join(' ')}] "
  end
end
ActionMailer::Base.register_interceptor(AddAppnameToEmailSubject)

2 Responses
Add your response

You could initialize the prefixes with company_name

def self.email_prefix
    prefixes = [COMPANY_NAME]
    ....
end
over 1 year ago ·

This code now has an official home in the email_prefixer gem!
https://github.com/wireframe/email_prefixer

over 1 year ago ·