【问题标题】:Not receiving confirmation mail with devise没有收到带有设备的确认邮件
【发布时间】:2015-03-05 03:16:00
【问题描述】:

按照教程,我将用户身份验证添加到基本的 Rails 应用程序。

有一个步骤我必须将以下代码添加到setup_mail.rb 文件中

if Rails.env.development?
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
    address:        'smtp.sendgrid.net',
    port:           '587',
    authentication: :plain,
    user_name:      ENV['SENDGRID_USERNAME'],
    password:       ENV['SENDGRID_PASSWORD'],
    domain:         'heroku.com',
    enable_starttls_auto: true
  }
end

我已经添加了 sendgrid 插件,并将用户 ID 和密码放在 application.yml 文件中。

但是当我注册时,我没有收到确认邮件。

还交叉检查了development.rb 文件并确保存在以下代码

....
   config.action_mailer.default_url_options = { host: 'localhost:3000' }
   config.action_mailer.delivery_method = :smtp
   config.action_mailer.perform_deliveries = true
end

按照教程逐字逐句。因此真的难住了!谷歌给我指出了几个地方,让我更加困惑!

这就是我的迁移数据库文件目前的样子

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Customization
       t.string :name
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
       t.string   :confirmation_token
       t.datetime :confirmed_at
       t.datetime :confirmation_sent_at
       t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is    :failed_attempts
  # t.string   :unlock_token # Only if unlock strategy is :email or :both
  # t.datetime :locked_at


  t.timestamps
end

add_index :users, :email,                unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token,   unique: true
# add_index :users, :unlock_token,         unique: true
  end
end

这也是我的 user.rb 文件

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable
end

这些是我按顺序执行的步骤

  1. 已安装设计 gem,运行 bundle
  2. rails g devise:install
  3. 将上述代码添加到 development.rb 文件中
  4. rails g devise:views
  5. rails g devise User
  6. 在迁移文件中取消注释confirmable`
  7. rake db:migrate
  8. 在 user.rb 中的 User 类中添加了:confirmable
  9. heroku addons:add sendgrid:starter
  10. heroku congif:get SENDGRID_USERNAME
  11. heroku config:get SENDGRID_PASSWORD
  12. 创建了 config/initializers/setup_mail.rb 并添加了上述邮件设置。
  13. 已安装 figaro。
  14. 向 application.yml 文件添加环境变量
  15. `figaro heroku:set -e production

更新

在 development.rb 中也添加了邮件设置,没有运气。

尝试将域从 domain: 'heroku.com' 更改为 domain: 'sendgrid.com',但没有成功。

这是服务器日志。邮件似乎已发送,但我在收件箱或垃圾邮件中找不到它

Sent mail to myemail@gmail.com (2070.9ms)                                                                                                                                                    
Date: Wed, 07 Jan 2015 13:52:05 +0000                                                                                                                                                             
From: please-change-me-at-config-initializers-devise@example.com                                                                                                                                  
Reply-To: please-change-me-at-config-initializers-devise@example.com                                                                                                                              
To: myemailgmail.com                                                                                                                                                                        
Message-ID: <54ad3a05d2b29_1ba3fb59082841433253@bloc-ruby-179559.mail>                                                                                                                            
Subject: Confirmation instructions                                                                                                                                                                
Mime-Version: 1.0                                                                                                                                                                                 
Content-Type: text/html;                                                                                                                                                                          
 charset=UTF-8                                                                                                                                                                                    
 Content-Transfer-Encoding: 7bit                                                                                                                                                                   

 <p>Welcome !</p>                                                                                                                                                            

<p>You can confirm your account email through the link below:</p>                                                                                                                                 

 <p><a href="http://localhost:3000/users/confirmation? confirmation_token=W3xuYfA8aq2obMBmffghfghQaDt">Confirm my account</a></p>                                                                          

   (45.0ms)  commit transaction                                                                                                                                                                   
Redirected to http://bloc-ruby-179559.apse1.nitrousbox.com/                                                                                                                                       
Completed 302 Found in 2872ms (ActiveRecord: 46.4ms)                                                                              

【问题讨论】:

  • 给我看你的用户类...!!!
  • 用用户类更新问题
  • 在 config/environments/development.rb 中设置您的邮件设置,然后重新启动您的 Rails 服务器
  • 试过了,没用。
  • 你正在使用哪个邮件服务!!!

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


【解决方案1】:

检查以确保 Sendgrid 没有暂停您的帐户。通过您的 Heroku 仪表板登录。然后点击您的应用程序。然后在插件下,单击 Sendgrid。这将带您到他们的仪表板。如果出现警告说您的帐户处于等待审核状态,您必须联系他们并让他们将其删除。

【讨论】:

    【解决方案2】:

    您正在以开发模式运行它。邮件已形成,可以在日志中看到,但未送达。默认情况下它们不会在开发模式下发送。要启用,请在 development.rb 中使用它

    config.action_mailer.perform_deliveries = true
    

    在 config/environments/development.rb 中使用以下代码

    config.action_mailer.smtp_settings = {
        :address              => "smtp.sendgrid.net",
        :port                 => 587,
        :domain               => "sendgrid.com",   
        :user_name            => ""
        :password             => ""
        :authentication       => 'plain',
        :enable_starttls_auto => true  }
    }
    

    【讨论】:

    • 感谢您的意见。但是我已经在 development.rb 文件中输入了那行代码,正如您在我的问题中看到的那样。
    【解决方案3】:

    在 mail_setup.rb 中,尝试将第一行更改为:

    if Rails.env.development? || Rails.env.production?
    

    【讨论】:

      【解决方案4】:

      :confirmable 遇到了同样的问题。我无法收到要发送的电子邮件,但我能够欺骗应用程序认为它已被发送并确认 - 因此登录。

      :confirmable info

      在控制台中:

      User.find(1).confirm       # returns true unless it's already confirmed
      User.find(1).confirmed?    # true/false
      User.find(1).send_confirmation_instructions # manually send instructions
      

      【讨论】:

        【解决方案5】:

        在我的情况下,我在 localhost 环境中使用我的 gmail 帐户,当我检查我的邮件时,我显示来自 google 的邮件说“由于“不太安全的应用程序被阻止”而导致“登录尝试被阻止”,您可以启用它需要遵循mail link中收到的指令

        【讨论】:

          【解决方案6】:

          在开发设置中,默认情况下不发送邮件

          我们可以通过设置启用 config.action_mailer.perform_deliveries = true

          如果还没有解决,我们可以设置

          config.action_mailer.raise_delivery_errors = true

          我们可以在控制台中找到打印的确切问题

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-06
            • 1970-01-01
            • 2018-03-25
            • 2015-07-06
            • 1970-01-01
            • 2017-05-03
            相关资源
            最近更新 更多