Can I specify a different recipient for the ActionMailer email based on environment?
I am wondering if it is possible to configure the Rails email received from ActionMailer to be sent to another recipient based on the environment. For example, for development, I would like him to send an email to my personal email address so that I don't litter our company's email with "Testing" emails; for production, however I want it to use the real address.
How can I achieve this?
source to share
By default, the development environment is not configured to send emails (it just logs them).
Setting up alternate accounts can be done in different ways. You can use some logic in your mailer like ...
recipients (Rails.env.production? ? "email@company.com" : "test@non-company.org")
Or, you can define the receiver as a constant in the environment files, for example:
/config/environment/production.rb
EMAIL_RECIPIENT = "email@company.com"
/config/environment/development.rb
EMAIL_RECIPIENT = "test@non-company.org"
and then use the constant in your mailer. Example:
recipients EMAIL_RECIPIENT
source to share
The mail_safe plugin can be killed a bit. A simple initializer will do
Rails 2.x
if Rails.env == 'development'
class ActionMailer::Base
def create_mail_with_overriding_recipients
mail = create_mail_without_overriding_recipients
mail.to = "mail@example.com"
mail
end
alias_method_chain :create_mail, :overriding_recipients
end
end
Rails 3.x
if Rails.env == 'development'
class OverrideMailReciptient
def self.delivering_email(mail)
mail.to = "mail@example.com"
end
end
ActionMailer::Base.register_interceptor(OverrideMailReciptient)
end
source to share