Search code examples
ruby-on-railsemailenvironment-variablesruby-on-rails-5mailer

How do I configure a "From" address in my Rails 5 mailer?


I'm trying to create an email from my Rails 5 app and I want to set the "From:" address per environment. So I have created config/initializers/mailer_config.rb whit this content

if defined?(Rails)
  case Rails.env
  when "production"
    RAILS_FROM_EMAIL = "no-reply@me.no-ip.com"
  when "test"
    RAILS_FROM_EMAIL = "no-reply@me.no-ip.com"
  when "development"
    RAILS_FROM_EMAIL = "no-reply@me.no-ip.com"
  end
end

Here is my mailer class, app/mailers/user_notifier.rb

class UserNotifier < ActionMailer::Base
  default :from => '#{RAILS_FROM_EMAIL}'

    ...

end

But when I go to test out my mailer, the from address doesn't seem to be getting picked up. It appears as

From:
#{RAILS_FROM_EMAIL}

What else do I need to configure to get my "From" address recognized?


Solution

  • default :from => '#{RAILS_FROM_EMAIL}'
    

    is not correct, RAILS_FROM_EMAIL is a variable so you can just call it like this.

    default :from => RAILS_FROM_EMAIL
    

    or even better

    default from: RAILS_FROM_EMAIL
    

    Also if you want to call a variable in a string like you have above you need to us " instead of ', it would look like "#{RAILS_FROM_EMAIL}". But you don't ever need to do that with one variable.