Rails: Devise not sending confirmation email in production (Heroku)

青春壹個敷衍的年華 提交于 2021-02-10 09:32:51

问题


I'm trying to set up email confirmation for new users for my site on Heroku. It works fine in development (it's sent from the correct email, even though I never specified it in development.rb)

Here is the code in development.rb: (I was using MailCatcher)

config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}

Here is the code in production.rb:

config.action_mailer.default_url_options = {:host => 'myapp.herokuapp.com'}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
   :address => "smtp.gmail.com",
   :port => 587,
   :domain => 'myapp.herokuapp.com',  #I've also tried changing this to 'gmail.com'
   :authentication => :plain, # I've also tried changing this to :login
   :enable_starttls_auto => true,
   :user_name => 'mygmailaccount@gmail.com',
   :password => 'mypassword'
 }

And here are the last few (of a very long list of) Heroku logs when I type "heroku logs" in the command prompt:

2013-11-24T18:51:01.069086+00:00 app[web.1]:   Rendered layouts/_header.html.erb
(1.4ms)
2013-11-24T18:50:57.403500+00:00 heroku[router]: at=info method=GET path=/users/
confirmation/new host=myapp.herokuapp.com fwd="118.22.125.86" dyno=we
b.1 connect=30ms service=18ms status=304 bytes=0
2013-11-24T18:50:32.655624+00:00 heroku[router]: at=info method=GET path=/users/
sign_up host=myapp.herokuapp.com fwd="118.22.125.86" dyno=web.1 conne
ct=25ms service=78ms status=304 bytes=0

The production site on Heroku just says "We're sorry, but something went wrong." The email never gets sent. The account with the specified email is never logged into the database.

Edit: Here are the log files after I added config.log_level = :debug

2013-11-24T20:35:31.602702+00:00 heroku[router]: at=info method=GET path=/favico
n.ico host=myapp.herokuapp.com fwd="118.22.125.86" dyno=web.1 connect
=2ms service=9ms status=304 bytes=0
2013-11-24T20:35:13.963743+00:00 heroku[router]: at=info method=GET path=/favico
n.ico host=myapp.herokuapp.com fwd="118.22.125.86" dyno=web.1 connect
=1ms service=447ms status=200 bytes=0

And another one:

2013-11-24T20:38:32.693982+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rac
k-1.4.5/lib/rack/handler/webrick.rb:59:in `service'
2013-11-24T20:38:32.694321+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/
webrick/server.rb:295:in `block in start_thread'
2013-11-24T20:38:32.693982+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rac
k-cache-1.2/lib/rack/cache/context.rb:51:in `call'
2013-11-24T20:38:32.693982+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/
webrick/httpserver.rb:94:in `run'
2013-11-24T20:38:32.693982+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/
webrick/httpserver.rb:138:in `service'

These are only the last few logs. The entire log is too long to copy on here. I'm not sure what to be looking for.


回答1:


I have a similar setup for a Rails app in Heroku that uses Gmail. Here's the differences...

The domain key in you ActionMailer smtp_settings shouldn't be necessary as that is only used for Google Apps for Business. Try removing that config variable and re-deploying your app.

Also, my config doesn't have the following:

config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true

I'd recommend removing those lines as well.

Finally, my default_url_options line has a key of protocol with a value of http. So, changing that config may help as well:

config.action_mailer.default_url_options = {:host => 'myapp.herokuapp.com', :protocol => 'http'}



回答2:


Production.rb

  config.action_mailer.delivery_method = :smtp

  config.action_mailer.perform_deliveries = true

  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.default_url_options = { :host => 'myapp.herokuapp.com', :protocol => 'http' }

  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'gmail.com',
    user_name:            'example@gmail.com',
    password:             'Your gmail password',
    authentication:       'plain'
  }

2nd Step: Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue.

Reason: Google was blocking access from unknown location.



来源:https://stackoverflow.com/questions/20179740/rails-devise-not-sending-confirmation-email-in-production-heroku

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