Matt Singer
Matt Singer

Reputation: 2157

How do I setup different ActionMailer Base smtp settings for development and production?

I have ActionMailer working correctly in both production and development. I use different smtp settings for each environment, gmail for development and a SendGrid account through Heroku for production. I manually switch the settings in the setup_mail.rb file to work in development and then switch them back before pushing into production. This prevents my gmail password from becoming public on github as the SendGrid/Heroku settings do not require my password in the file:

development setup_mail.rb

    ActionMailer::Base.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :domain               => "mysite.com",
      :user_name            => "[email protected]",
      :password             => 'mypassword',
      :authentication       => "plain",
      :enable_starttls_auto => true
}

production setup_mail.rb

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp

I am concerned that I will accidentally push the development settings with my password to github. I'd like to stop switching settings manually to prevent this from happening. How do I setup different ActionMailer Base smtp settings for development and production? Thanks

Upvotes: 0

Views: 4231

Answers (1)

Leonel Galán
Leonel Galán

Reputation: 7167

Have this setting in production.rb and development.rb, instead of having your password hard coded, you could use environment variables locally too, create a .env file in your project, which will be loaded when you cd in:

[email protected]
EMAIL_PASSWORD= mypassword

Use ENV['EMAIL'] AND ENV['EMAIL_PASSWORD'] in development.rb

Upvotes: 2

Related Questions