Heroku/devise - Missing host to link to! Please provide :host parameter or set default_url_options[:host]

≯℡__Kan透↙ 提交于 2019-11-26 21:26:14
thenengah

You need to add this to your environment.rb

  config.action_mailer.default_url_options = { :host => 'localhost' }

Make sure you change host to your production url and keep it localhost for development. This is for the mailer, it needs a default email to send out notices such as confirmations etc...


You should check the logs on the heroku server heroku logs run that from the console and it will tell you the exact error.

When you push to heroku you need to configure the environment.rb file with the heroku subdomain:

config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }

Depending upon version, this should go in production.rb, not environment.rb.

Ok,

First you have to install the sendgrid gem with this command line:

heroku addons:add sendgrid:free

Then you just have to configure your env/dev.rb and env/prod.rb like this:

env/dev.rb

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

env/prod.rb

config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }

Push on git and heroku. It should work..

Codeglot's anwser above does the job, but we wanted something a bit more flexible, so we did this:

On Heroku, we run multiple Production environments for staging and testing, so we need a flexible solution for the production.rb environment file.

In production.rb

config.action_mailer.default_url_options = { :host => ENV['MAILER_URL'] }

Then set the MAILER_URL environment variable for your app like so

heroku config:set MAILER_URL=my-awesome-app.herokuapp.com --app my-awesome-app

If you're running on Cedar:

  1. run heroku addons:add sendgrid:free from your console.

  2. Add the following lines to config/environments/production.rb in your app.

.

  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

  config.action_mailer.default_url_options = { :host => 'YOUR-DOMAIN-HERE.COM' }

I had to do a number of things to get it to work in the production environment: Inside of my production.rb file (/config/environments/production.rb) I added the following:

Rails.application.routes.default_url_options[:host] = 'myappsname.herokuapp.com'
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"

This is with Rails 4 and Devise 3

Here's a trick to consider. It will make it easier to switch server and environment, and changing domain as in a custom domain at heroku.

Instead of hard-coding the host name, read it from the request. Here's an example of a simple app I have.

class MyMailController < ApplicationController
  before_filter :set_host_from_request, only: [:create]

  ....   

  private
  def set_host_from_request
    ActionMailer::Base.default_url_options = { host: request.host_with_port }
  end
end

in the simple example I have only one action, create, that results in emails being sent. You can add the before_filter in application_controller.rb without the excludes to make it always store the host name.

PRO:

  • always get the correct host name in the URL of emails you send
  • Having the default_url_options at our staging server configured for production, resulted in emails being sent to test users with links to production (they click them of course). No damage, but very time consuming.

CON:

without the default_url_options you cannot send manually in console

#config.action_mailer.default_url_options = { :host => 'mydomain.com' }
$rails console
  User.invite!(email: "ceo@example.com")
ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
...stacktrace

If you can see drawbacks that I can't, please share! Thanks

The working one after so many research,

  1. Don't forget to add default from: mail address in your ApplicationMailer (application_mailer.rb) as,

    class ApplicationMailer < ActionMailer::Base
      default from: 'yourmail@gmail.com'
      layout 'mailer'
    end
    
  2. Add the below configuration in your production.rb.

    config.action_mailer.default_url_options = 
      { :host => 'yourapp.herokuapp.com' }
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 587,
      domain:               'heroku.com',
      user_name:            'yourmail@gmail.com',
      password:             'yourgmailpassword',
      authentication:       'login',
      enable_starttls_auto: true
    }
    
  3. Enable IMAP from your Gmail settings in Forwarding IMAP/POP tab.

  4. Allow less secure apps: ON from https://myaccount.google.com/lesssecureapps

You're now good to go. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!