Rails 4 with Devise on Heroku - 发送邮件的问题

Rails 4 with Devise on Heroku - Issue with sending mail

我终于在开发中正确发送了电子邮件确认(使用 Devise 进行身份验证等),但在生产中我根本无法让它工作。

UPDATE/EDIT #1:

在尝试了下面的建议后不幸的是没有解决问题,然后我修改了我的生产设置如下:

UPDATE/EDIT #2:

试图修改 :domain 值:

  Rails.application.routes.default_url_options[:host] = 'obscure-crag-6843.herokuapp.com'

      config.action_mailer.raise_delivery_errors = true
      config.action_mailer.perform_deliveries = true
      config.action_mailer.delivery_method = :smtp
      config.action_mailer.default :charset => "utf-8"

      config.action_mailer.smtp_settings = {
        :address                  => 'smtp.sengrid.net',
        :port                     => 587,
        :authentication           => 'plain',
        :user_name                => ENV['SENDGRID_USERNAME'],
        :password                 => ENV['SENDGRID_PASSWORD'],
        :domain                   => 'obscure-crag-6843.heroku.com',
        :enable_starttls_auto     => true
      }
       # Enable locale fallbacks for I18n (makes lookups for any locale fall back to

      # the I18n.default_locale when a translation cannot be found).
      config.i18n.fallbacks = true

      # Send deprecation notices to registered listeners.
      config.active_support.deprecation = :notify

      # Use default logging formatter so that PID and timestamp are not suppressed.
      config.log_formatter = ::Logger::Formatter.new

      # Do not dump schema after migrations.
      config.active_record.dump_schema_after_migration = false

仍然出现错误:

 2015-02-02T16:52:39.634893+00:00 app[web.1]: Processing by Devise::ConfirmationsController#create as HTML
    2015-02-02T16:52:39.624339+00:00 app[web.1]: Started POST "/users/confirmation" for 66.108.200.119 at 2015-02-02 16:52:39 +0000
    2015-02-02T16:52:39.634923+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"3yPKp3FqN2Mfxg+s28xQDpLhWtkLRFt1ZxfygyM5JNKCGlo1+nsx0dXy0NbSt7D80TvKIuPfSESziGQ/L9HoiQ==", "user"=>{"email"=>"groundhog.nice@gmail.com"}, "c
    ommit"=>"Resend confirmation instructions"}
    2015-02-02T16:52:39.673202+00:00 app[web.1]:   User Load (12.2ms)  SELECT  "users".* FROM "users" WHERE "users"."email" =   ORDER BY "users"."id" ASC LIMIT 1  [["email", "groundhog.nice@gmail.com"]]
    2015-02-02T16:52:39.673202+00:00 app[web.1]:   User Load (12.2ms)  SELECT  "users".* FROM "users" WHERE "users"."email" =   ORDER BY "users"."id" ASC LIMIT 1  [["email", "groundhog.nice@gmail.com"]]
    2015-02-02T16:52:39.928958+00:00 app[web.1]:   User Load (3.5ms)  SELECT  "users".* FROM "users" WHERE "users"."confirmation_token" =   ORDER BY "users"."id" ASC LIMIT 1  [["confirmation_token", "d00c657be3922503cb350e6db9e5b20ac6daa2
    a8aa401fb346d504d0e2d89f2e"]]
    2015-02-02T16:52:39.941419+00:00 app[web.1]:   SQL (3.2ms)  UPDATE "users" SET "confirmation_token" = , "confirmation_sent_at" = , "updated_at" =  WHERE "users"."id" =   [["confirmation_token", "d00c657be3922503cb350e6db9e5b20ac
    6daa2a8aa401fb346d504d0e2d89f2e"], ["confirmation_sent_at", "2015-02-02 16:52:39.929928"], ["updated_at", "2015-02-02 16:52:39.933892"], ["id", 1]]
    2015-02-02T16:52:40.047348+00:00 app[web.1]:   Rendered devise/mailer/confirmation_instructions.html.erb (7.5ms)
    2015-02-02T16:52:39.933525+00:00 app[web.1]:    (3.2ms)  BEGIN
    2015-02-02T16:52:39.949859+00:00 app[web.1]:    (7.8ms)  COMMIT
    2015-02-02T16:52:40.300090+00:00 app[web.1]: Devise::Mailer#confirmation_instructions: processed outbound mail in 347.2ms
    2015-02-02T16:52:40.300084+00:00 app[web.1]:
    2015-02-02T16:53:09.608907+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/users/confirmation" host=obscure-crag-6843.herokuapp.com request_id=88102416-e8e8-4e2d-904e-61505b7cc80d fwd="66.108.200.119" d
    yno=web.1 connect=2ms service=30001ms status=503 bytes=0
    2015-02-02T16:53:10.591515+00:00 app[web.1]: From: please-change-me-at-config-initializers-devise@example.com
    2015-02-02T16:53:10.591525+00:00 app[web.1]: Mime-Version: 1.0
    2015-02-02T16:53:10.591522+00:00 app[web.1]: Message-ID: <54cfab584a63e_33f8391b77e986273@5849319e-9d85-442b-b13b-b79cd9373d33.mail>
    2015-02-02T16:53:10.591526+00:00 app[web.1]: Content-Type: text/html;
    2015-02-02T16:53:10.539928+00:00 app[web.1]: Sent mail to groundhog.nice@gmail.com (30239.5ms)
    2015-02-02T16:53:10.591510+00:00 app[web.1]: Date: Mon, 02 Feb 2015 16:52:40 +0000
    2015-02-02T16:53:10.591520+00:00 app[web.1]: To: groundhog.nice@gmail.com
    2015-02-02T16:53:10.539921+00:00 app[web.1]:
    2015-02-02T16:53:10.591518+00:00 app[web.1]: Reply-To: please-change-me-at-config-initializers-devise@example.com
    2015-02-02T16:53:10.591532+00:00 app[web.1]: <p>Welcome groundhog.nice@gmail.com!</p>
    2015-02-02T16:53:10.591528+00:00 app[web.1]:  charset=UTF-8
    2015-02-02T16:53:10.591523+00:00 app[web.1]: Subject: Confirmation instructions
    2015-02-02T16:53:10.591528+00:00 app[web.1]:  charset=UTF-8                                                                                                                                                                       [115/1420]
    2015-02-02T16:53:10.591523+00:00 app[web.1]: Subject: Confirmation instructions
    2015-02-02T16:53:10.591529+00:00 app[web.1]: Content-Transfer-Encoding: 7bit
    2015-02-02T16:53:10.591912+00:00 app[web.1]: Completed 500 Internal Server Error in 30957ms
    2015-02-02T16:53:10.591534+00:00 app[web.1]: <p>You can confirm your account email through the link below:</p>
    2015-02-02T16:53:10.591535+00:00 app[web.1]:
    2015-02-02T16:53:10.591537+00:00 app[web.1]: <p><a href="http://obscure-crag-6843.herokuapp.com/users/confirmation?confirmation_token=c9agbLB_yT57z4shtjZP">Confirm my account</a></p>
    2015-02-02T16:53:10.591530+00:00 app[web.1]:
    2015-02-02T16:53:10.591533+00:00 app[web.1]:
    2015-02-02T16:53:10.591538+00:00 app[web.1]:
    2015-02-02T16:53:10.630637+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/net/smtp.rb:520:in `start'
    2015-02-02T16:53:10.630628+00:00 app[web.1]: Net::OpenTimeout (execution expired):
    2015-02-02T16:53:10.630630+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/net/smtp.rb:541:in `initialize'
    2015-02-02T16:53:10.630631+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/net/smtp.rb:541:in `open'
    2015-02-02T16:53:10.630633+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/net/smtp.rb:541:in `tcp_socket'
    2015-02-02T16:53:10.630634+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/net/smtp.rb:551:in `block in do_start'
    2015-02-02T16:53:10.630635+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/net/smtp.rb:550:in `do_start'
    2015-02-02T16:53:10.630623+00:00 app[web.1]:
    2015-02-02T16:53:10.630645+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'

在这一点上,我要么尝试使用不同的邮件提供商,要么暂时回到 Gmail,当然,我最终将不得不处理这个问题。如果有人有更多的建议那就太好了。

我认为是这一行:

    :address                  => 'smtp.sengrid.net',

我注意到您在代码中使用了 :domain 属性。虽然我以前从未使用过,但您可以尝试将域更改为您的 app-name.heroku.com 而不是 heorku.com 吗?我将向您展示我自己如何使用邮寄代码:

    config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["gmail_username"],
  password: ENV["gmail_password"]
  }

祝你好运:)

我在生产环境(devise、heroku)中发送电子邮件时也遇到了问题,但我能够解决它。将我的代码设置与您的进行比较,我发现这些行不同:

Rails.application.routes.default_url_options[:host] = 'obscure-crag-6843.herokuapp.com'

:domain => 'obscure-crag-6843.heroku.com',

:address => 'smtp.sengrid.net',

Rails.application.configure do    

我有

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

并在

config.action_mailer.smtp_settings    

一切都一样,除了

:address => 'smtp.sendgrid.net', (you misspelled sendgrid)

:domain => 'heroku.com',

当然,您需要将 Sendgrid 添加到 Heroku。希望这对您有所帮助!