Send mail using mailer when registering as a devise user (welcome mail) Reference: https://web-camp.io/magazine/archives/19143 Reference: https://freesworder.net/rails-mail/
■ Improvement of UX ■ Improved application perfection
■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7
mac.terminal
$ rails new mail
$ cd mail
In the app.gemfile
gem 'devise' #Skip emails when registering as a devise member
gem 'dotenv-rails' #Used for environment variables. Details will be described later
mac.terminal
$ bundle install #Install gemfile
$ rials g devise:install #Initialize devise
$ rails g devise User
$ rails g devise:views
$ rails g devise:controllers users
db/migrate/xxxxxxxxxx_devise_create_users.rb
# Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable ← added
end
/.env
mail = 'Your gamil address'
password = 'Password application required'
config/initializers/mail_config.rb
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'smtp.gmail.com',
domain: 'gmail.com',
port: 587,
user_name: ENV['mail'],
password: ENV['password'],
authentication: 'plain',
enable_starttls_auto: true
}
confirmation_instructions.html
##It can be changed arbitrarily and the content of the email will be changed.
<p>Welcome <%= @email %>!</p>
<p>You can confirm your account email through the link below:</p>
##So far
<!-- <p><%#= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p> -->
#Comment out,Because it is unnecessary this time
mac.terminal
$ rails db:migrate
$ rails s -b 0.0.0.0
The email should have been sent when you registered as a user.
It took about 3 hours via action mailer etc. It was easier to implement than I expected.
Recommended Posts