[RUBY] Fastest rails mail devise Welcome mail sending function implementation action mailer unnecessary Easyest

【goal】

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/

【merit】

■ Improvement of UX ■ Improved application perfection

【Development environment】

■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7

【Implementation】

Create application

mac.terminal


$ rails new mail

$ cd mail

gemfile added

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

Return to the terminal and create various things

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

Edit DB, migrationfile and remove the comment below.

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

edit model

user.rb


class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable ← added

end

Add the .env file to the root directory and add the following

/.env


mail = 'Your gamil address'
password = 'Password application required'

Create and edit config / initializers / mail_config.rb

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
}

Edit view / users / mailer / confirmation_instructions.html

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

To the terminal again

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

Fastest rails mail devise Welcome mail sending function implementation action mailer unnecessary Easyest
[Rails 6] Implementation of inquiry function using Action Mailer
Implemented mail sending function with rails
Ruby on Rails Email automatic sending function implementation
Implementation of Ruby on Rails login function (devise edition)
Rails Action Text implementation
Rails search function implementation
Rails fuzzy search function implementation
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of new registration function in wizard format using devise