Not receiving confirmation mail with devise

↘锁芯ラ 提交于 2019-12-13 17:47:22

问题


Following a tutorial where I am adding user authentication to a basic rails app.

There is a step where I have to add the following code to a setup_mail.rb file

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

I have added the sendgrid addon, and put user id and password on the application.yml file as well.

But when I sign up, I do not receive a confirmation mail.

cross checked the development.rb file as well and made sure the following code was present

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

Followed the tutorial word to word. Hence really stumped! Google pointed me to a few places and confused me futher!

This is how my migration db file looks like at the moment

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

Here is my user.rb file as well

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

These are the steps I followed in order

  1. Installed devise gem, ran bundle
  2. rails g devise:install
  3. Added the above code to development.rb file
  4. rails g devise:views
  5. rails g devise User
  6. Uncommented confirmable in the migration file`
  7. rake db:migrate
  8. Added :confirmable to the User class in user.rb
  9. heroku addons:add sendgrid:starter
  10. heroku congif:get SENDGRID_USERNAME
  11. heroku config:get SENDGRID_PASSWORD
  12. Created config/initializers/setup_mail.rb and added the above mail settings.
  13. Installed figaro.
  14. Added environment variables to application.yml file
  15. `figaro heroku:set -e production

Update

Added mail settings in development.rb as well, no luck.

Tried changing the domain from domain: 'heroku.com' to domain: 'sendgrid.com', no luck.

Here is the server log. It appears that the mail has been sent but I cannot find it in the inbox or spam

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)                                                                              

回答1:


Check to make sure Sendgrid doesn't have your account on hold. Log in through your Heroku dashboard. Then click on your app. Then under addons, click on Sendgrid. This will take you to their dashboard. If there is a warning saying that your account is on hold pending review, you'll have to contact them and have them remove it.




回答2:


You are running it for development mode. Mails are formed and can be seen in logs but not delivered. They will not be sent in dev mode by default.To enable, use this in development.rb

config.action_mailer.perform_deliveries = true

use the below code in 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  }
}



回答3:


In mail_setup.rb, try changing the first line to:

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



回答4:


I ran into the same problem with :confirmable. I was not able to get the email to send, but I was able to trick the app into thinking that it had been sent and confirmed - and therefore login.

:confirmable info

In the console:

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:


In my case, i was using my gmail account with localhost environment, when i check my mail, i show a mail from google saying "Sign-in attempt was blocked" due to "Less secure app blocked" , you can enable it need to follow instruction as received in mail link



来源:https://stackoverflow.com/questions/27815873/not-receiving-confirmation-mail-with-devise

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