在Heroku上使用Devise的Rails 4 - 发送邮件的问题

时间:2015-02-02 04:34:53

标签: ruby-on-rails ruby ruby-on-rails-4 heroku devise

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

更新/编辑#1:

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

更新/编辑#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" = $1  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" = $1  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" = $1  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" = $1, "confirmation_sent_at" = $2, "updated_at" = $3 WHERE "users"."id" = $4  [["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,但当然我最终还是要处理这个问题。如果有人有任何更好的建议。

3 个答案:

答案 0 :(得分:1)

我认为这就是这条线:

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

答案 1 :(得分:1)

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

  

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

     

:domain =&gt; 'obscure-crag-6843.heroku.com',

     

:address =&gt; 'smtp.sengrid.net',

Rails.application.configure do    

我有

  

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

并在

config.action_mailer.smtp_settings    

除了

之外,一切都是一样的
  

:address =&gt; 'smtp.sendgrid.net',(你拼错了sendgrid)

     

:domain =&gt; 'heroku.com',

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

答案 2 :(得分:0)

我注意到您在代码中使用了: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"]
  }
祝你好运:)