Devise Confirmable with SendGrid

有 N 人看过

After setting up Devise gem, add default_url_option to development.rb.

config/environments/development.rb

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

Generate a User model

$ rails generate devise User

Uncomment confirmable fields in newly created migration file
db/migrate/2020xxxxxxx_devise_create_users.rb

## Confirmable
t.string   :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string   :unconfirmed_emailreconfirmable

Add :confirmable to User.rb

app/models/user.rb

   devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :validatable,
          :confirmable

In deveise.rb, configurate sender’s email and reconfirmable state.

  # this email have to be the same as SendGrid Sender Identity
  config.mailer_sender = "example@gmail.com"

config/environments/development.rb

  # SendGrid
ActionMailer::Base.smtp_settings = {
  :user_name => Rails.application.credentials.dig(:sendgrid, :username),
  :password => Rails.application.credentials.dig(:sendgrid, :password),
  :domain => 'your domain',
  :address => 'smtp.sendgrid.net',
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}

Rails.application.credentials.sendgrid(:username) does not work here!

Store you credentials in master.key

$ EDITOR='code --wait' rails credentials:edit
 sendgrid:
   username: "apikey" # leave it as is
   password: "Your API key"

Errors I ran into:

log/production.log

I, [2021-06-28T02:29:03.586863 #1835748]  INFO -- : [46c46c7d-2f67-4def-9e9f-7a64dd5d5493] Completed 500 Internal Server Error in 305ms (ActiveRecord: 2.0ms | Allocations: 1831)
F, [2021-06-28T02:29:03.589058 #1835748] FATAL -- : [46c46c7d-2f67-4def-9e9f-7a64dd5d5493]
[46c46c7d-2f67-4def-9e9f-7a64dd5d5493] ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):
[46c46c7d-2f67-4def-9e9f-7a64dd5d5493]     2:
[46c46c7d-2f67-4def-9e9f-7a64dd5d5493]     3: <p>You can confirm your account email through the link below:</p>
[46c46c7d-2f67-4def-9e9f-7a64dd5d5493]     4:
[46c46c7d-2f67-4def-9e9f-7a64dd5d5493]     5: <p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
[46c46c7d-2f67-4def-9e9f-7a64dd5d5493]

Forgot to configure default_url_options in environments/production.rb

Net::OpenTimeout (execution expired)

log/production.log

I, [2021-06-28T03:09:52.753696 #1845775]  INFO -- : [d23e7eed-eb39-4a53-a330-90bc7719cad6] Delivered mail 60d8cce2b74c7_1c2a0f53fc980c@localhost.mail (30003.8ms)
I, [2021-06-28T03:09:52.754019 #1845775]  INFO -- : [d23e7eed-eb39-4a53-a330-90bc7719cad6] Completed 500 Internal Server Error in 30014ms (ActiveRecord: 4.1ms | Allocations: 2705)
F, [2021-06-28T03:09:52.755417 #1845775] FATAL -- : [d23e7eed-eb39-4a53-a330-90bc7719cad6]
[d23e7eed-eb39-4a53-a330-90bc7719cad6] Net::OpenTimeout (execution expired):
[d23e7eed-eb39-4a53-a330-90bc7719cad6]

Need to contact VPS support

Note:

  • Configure SendGrid using SMTP relay.
  • SendGrid Sender Identity must be setup in order for this to work
  • Must use the verified email (as Sender Identity) on the config.mailer_sender in devise.rb
  • Linode have SMTP port restrictions in place on all Linodes by default, need to open up a support ticket to unblock the port

References:
Module: Devise::Models::Confirmable — Documentation for heartcombo/devise (master)
ActionMailer - E-mail 發送 - Rails 實戰聖經
卡卡米的記憶體: Ruby - 使用 Devise confirmable

Unable to send mail from my rails web application | Linode Questions
Devise and SendGrid integration into rails 6 - DEV Community

本作品采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 进行许可。