How to set up mailer in Rails app for production environment on Heroku
Asked Answered
G

3

18

I need to use a mailer for sending out emails to users to set their passwords to the "recoverable" function of Devise and active admin. On the development environment I have done this by adding the following to these files:

config/environments/development

#Added per active admin install instructions
config.action_mailer.default_url_options = { :host => 'localhost:3000' }


#These settings are for the sending out email for active admin and consequently the   devise mailer
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = 
{

  :address            => 'smtp.gmail.com',
  :port               => 587,
  :domain             => 'gmail.com', #you can also use google.com
  :authentication     => :plain,
  :user_name          => '[email protected]',
  :password           => 'XXXXXXX'
}

How do I get the same functionality for the production environment? I want to deploy my app to Heroku. What files and code would I need to add?

Goosegog answered 17/8, 2012 at 0:14 Comment(4)
You need that same code in config/environments/production.rbDynamite
Or put it in config/application.rb if the same for all of your environmentsDynamite
Yes but what should I put for this in production: config.action_mailer.default_url_options = { :host => '????' }Goosegog
the url for your application config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' } or 'yourcustomdomain.com' if using oneDynamite
P
4

If it works in development mode, then it will work in production mode.

Supposing everything is setup correctly, resetting a password in development will already send an actual email using your gmail account.

Devise only relies on the mailer config setup correctly (which you have done), and configuring devise to allow password reset, and possibly another setting for the From field of the email.

Pericarditis answered 17/8, 2012 at 0:51 Comment(2)
Yes but what should I put for this in production: config.action_mailer.default_url_options = { :host => '????' }Goosegog
you need to put your application's host url, for example in production.rb write config.action_mailer.default_url_options = { host: 'my-app.herokuapp.com' }Doody
I
13

All configurations you have set in Development mode will work EXCEPT you will need to reconfigure the default mailer url.

So.

  1. Copy-paste your settings from development.rb.

  2. Point your default mailer to your heroku app:

    config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
    

Also, be careful of any email limits your smtp may have when moving to production. It's hard to trigger gmail's smtp limits while developing, for example, but they could be more easily triggered in production.

Iquique answered 26/5, 2014 at 22:5 Comment(0)
P
4

If it works in development mode, then it will work in production mode.

Supposing everything is setup correctly, resetting a password in development will already send an actual email using your gmail account.

Devise only relies on the mailer config setup correctly (which you have done), and configuring devise to allow password reset, and possibly another setting for the From field of the email.

Pericarditis answered 17/8, 2012 at 0:51 Comment(2)
Yes but what should I put for this in production: config.action_mailer.default_url_options = { :host => '????' }Goosegog
you need to put your application's host url, for example in production.rb write config.action_mailer.default_url_options = { host: 'my-app.herokuapp.com' }Doody
M
4

This should work fine!

As long as config/environments/production.rb has the same thing with an exception. The default_url_options should have a :host value of 'localhost' only in development and 'YOURAPPNAME.herokuapp.com' in heroku production.

i.e.

config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }

Remember to unlock captcha on gmail, otherwise it won't send email from heroku (unknown source). You can do that by going to this link: http://www.google.com/accounts/DisplayUnlockCaptcha

Just as a suggestion, I'd say move this from environments.rb

ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true

and place is in environments/development.rb as

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

It's not needed in production.

See Net::SMTPAuthenticationError when sending email from Rails app (on staging environment) for more information in regards to gmail seeing heroku as an unknown host.

Mansard answered 30/7, 2015 at 14:23 Comment(1)
I have done exactly this but am not able to send gmail emails in production on heroku. Has something changed?Lorgnon

© 2022 - 2024 — McMap. All rights reserved.