Implement a function to automatically send a confirmation email to the registered email address.
Make an app called sample.
$ rails new sample
Move directory.
$ cd sample
We will create an application to send to the newly added email address, so create a User table. This time we will have two columns.
$ rails g scaffold User name email
Create a database.
$ rails db:migrate
I will set it so that I can send Gmail. ①config/environments/development.rb ②config/environments/production.rb Please add the following to ① and ②.
Rails.application.configure do
config.action_mailer.raise_delivery_errors = true #Change from false to true
#Omission#
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
domain: 'smtp.gmail.com',
port: 587,
user_name: Rails.application.credentials.gmail[:user_name],
password: Rails.application.credentials.gmail[:password],
authentication: 'login',
enable_starttls_auto: true
}
end
It plays a role as a controller in sending emails.
The name of the mailer is SampleMailer.
$ rails g mailer SampleMailer
When executed, the following two mailers will be created under app / mailers.
① application_mailer "Setting of the whole mailer" ② sample_mailer "Setting of an individual mailer called SampleMailer created earlier"
① application_mailer "Setting of the whole mailer"
app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "Yamada Taro", #Sender's name
reply_to: Rails.application.credentials.gmail[:user_name] #Sender's email address
layout 'mailer'
end
② sample_mailer "Individual mailer settings"
app/mailers/sample_mailer.rb
class PostMailer < ApplicationMailer
default from: "Yamada Taro"
def published_email(user)
@user = user
mail to: user.email #Newly registered email address
end
end
The body of the email is created under app / views / sample_mailer. There are two types of email body, "HTML version" and "text version".
This time, we will create this. ① published_email.html.erb "HTML version" ② published_email.tex.erb "text version"
html:app/views/sample_mailer/published_email.html.erb
<!doctype html>
<html lang="ja">
<head>
<meta content="text/html; charset=UTF-8" />
</head>
<body>
<h2><%= @user.name %>Mr</h2>
<hr />
<p>
Hello!<%= @user.name %>San!</p>
<hr />
</body>
</html>
html:app/views/sample_mailer/published_email.text.erb
===============================
<%= @user.name %>Mr
===============================
Hello!<%= @user.name %>Mr.
Add a line to the "create method" of the controller.
app/controllers/users_controller.rb
class UsersController < ApplicationController
#Omission
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
SampleMailer.published_email(@user).deliver #Add this.
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
#Omission
end
Finally, set the routing and you're done.
config/routes.rb
Rails.application.routes.draw do
resources :users
root "users#index" #Add this.
end
$ rails s
The screen will come out like this! Please actually operate and check.
If you register your name and email address, an email will be sent automatically.
This is the end of implementation. Thank you for watching.
Recommended Posts