186

I am trying to push my app on heroku. I am still in dev. I use devise with the confirmable module.

When I try to add a user with the heroku console I got this error:

Missing host to link to! Please provide :host parameter or set default_url_options[:host]

in test and dev environment i have the following line:

environments/development.rb and environments/test.rb

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

I don't have set up something in the production environment.

I've tried to push with

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

but it doesn't work too..

I see on the web that it could be related to ActionMailer but I don't know what I have to configure. Many thanks for your idea!

EDITED:

Hi,

In order to not make my app crashes when I push on heroku I put this in my env/test.rb and my env/dev.rb (not in env.rb I think it is because it's a rails 3 app)

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

But when I tried to create a user in the heroku console:

User.create(:username => "test", :email => "test@test.com", :password => "test1234", :password_confirmation => "test1234", :confirmed_at => "2010-11-03 14:11:15.520128")

here are errors I got:

ActionView::Template::Error: Missing host to link to! Please provide :host parameter or set default_url_options[:host]
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:473:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_view/helpers/url_helper.rb:99:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:195:in `user_confirmation_url'

EDITED (2)

When I type heroku logs on console I got this ==> production.log <== So I think when one deploys on heroku it's already in production.

I configure the env/prod.rb like this:

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

and now I have this as error when I try to create a User:

Errno::EAFNOSUPPORT: Address family not supported by protocol - socket(2)
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `initialize'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `open'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `do_start'
/usr/ruby1.8.7/lib/ruby/1.8/timeout.rb:62:in `timeout'
Community
  • 1
  • 1
benoitr
  • 6,025
  • 7
  • 42
  • 67

6 Answers6

253

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.

sarnold
  • 102,305
  • 22
  • 181
  • 238
thenengah
  • 42,557
  • 33
  • 113
  • 157
  • Hi sam! I am sorry but i didn't get it. I just add the line config.action_mailer.default_url_options = { :host => 'localhost' } in my env.rb but my rails 3 app crashes on heroku. Did I have to put something on the env/prod.rb too? (sorry my english is so so-so..) – benoitr Nov 06 '10 at 20:37
  • Also ,did I have to put my rails app in production mode? Thanks! – benoitr Nov 06 '10 at 20:56
  • Hi Sam, I added also some info in the body of my question. Your answer helps but I can't still register a user via heroku console. I would be pleased if you can tell me what you thinks on this! Thanks! – benoitr Nov 07 '10 at 14:25
  • 1
    `:host => 'yourapp.heroku.com' ` this should be your domain. For example, if your domain is yahoo.com, then it should be `yahoo.heroku.com` or if you already setup custom domains it will just be your url `yahoo.com` – thenengah Nov 07 '10 at 16:07
  • 1
    Thanks Sam, I find the solution. The fact is that I didn't have the sendgrid addon and my smtp hasn't configured. Many thanks for all your backup! – benoitr Nov 07 '10 at 16:13
  • lol. No problem, now you can get devise going on all you apps :) – thenengah Nov 07 '10 at 16:49
  • 1
    Isn't it going to be `config.action_mailer.default_url_options = { :host => 'yourapp.herokuapp.com'`, as opposed to just `config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com'` ? – maudulus Apr 22 '14 at 00:32
  • You should set exact address to your app: if it is available at `yourapp.herokuapp.com`, just set `config.action_mailer.default_url_options = { host: 'yourapp.herokuapp.com' }`, otherwise devise will generate incorrect link ex. in confirmation email. – sampi Dec 04 '14 at 19:03
41

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..

benoitr
  • 6,025
  • 7
  • 42
  • 67
  • I get this error when I reference config.action_mailer in production.rb: /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.7/lib/ rails/railtie/configuration.rb:77:in `method_missing': undefined method `action_mailer' for # (NoMethodError) – Laran Evans May 12 '11 at 05:39
  • I am not sure I can help here... Did you reference it inside the block? (Website::Application.configure do ... config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' } ... end) – benoitr May 15 '11 at 02:52
  • 6
    now the sendgrid command is heroku addons:add sendgrid:starter (view usage details at http://devcenter.heroku.com/articles/sendgrid) – Danny Oct 27 '11 at 14:33
24

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
Ross
  • 1,934
  • 2
  • 20
  • 19
18

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' }
Mike
  • 9,692
  • 6
  • 44
  • 61
14

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

maudulus
  • 10,627
  • 10
  • 78
  • 117
  • 1
    This structure of the `default_url_options` caused an error which made my precompiling assets fail, just fyi. When I changed it back to ` = { host: 'myappsname.herokuapp.com' }` it worked. – Liz Jun 05 '16 at 17:03
1

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. :)

Anshab M K
  • 11
  • 1