Ruby on Rails Email automatic sending function implementation


Implement a function to automatically send a confirmation email to the registered email address.


① App creation

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

② Email transmission settings

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
  config.action_mailer.delivery_method = :smtp 
  config.action_mailer.smtp_settings = {
    address: '',
    domain: '',
    port: 587,
    authentication: 'login',
    enable_starttls_auto: true

③ Create mailer

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"

③ Mailer editing

① application_mailer "Setting of the whole mailer"


class ApplicationMailer < ActionMailer::Base
  default from:     "Yamada Taro",  #Sender's name
          reply_to:[:user_name] #Sender's email address
  layout 'mailer'

② sample_mailer "Individual mailer settings"


class PostMailer < ApplicationMailer
  default from: "Yamada Taro"
  def published_email(user)
    @user = user
    mail to:  #Newly registered email address

④ Create the body of the email

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"


<!doctype html>
<html lang="ja">
  <meta content="text/html; charset=UTF-8" />
  <h2><%= %>Mr</h2>
  <hr />
Hello!<%= %>San!</p>
  <hr />


<%= %>Mr
Hello!<%= %>Mr.

⑥ Edit controller

Add a line to the "create method" of the controller.


class UsersController < ApplicationController
  def create
    @user =
    respond_to do |format|
        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 }
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }

⑥ Edit routing

Finally, set the routing and you're done.


Rails.application.routes.draw do
  resources :users
  root "users#index"  #Add this.

Let's start it up and check it at the 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. 9.png

This is the end of implementation. Thank you for watching.

Recommended Posts

Ruby on Rails Email automatic sending function implementation
Ruby on Rails Email automatic sending function setting (using gmail)
[Ruby on Rails] Comment function implementation
[Ruby on Rails] Follow function implementation: Bidirectional
[Ruby on rails] Implementation of like function
Implementation of Ruby on Rails login function (Session)
Ruby on Rails address automatic input implementation method
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Ruby on Rails] CSV output function
[Ruby on Rails] DM, chat function
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Logical deletion (withdrawal function)
Validation settings for Ruby on Rails login function
Ruby on Rails Elementary
Ruby on Rails basics
Rails search function implementation
Ruby On Rails Association
[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Asynchronous communication of posting function, ajax
[Ruby on Rails] Posting score ranking function (whole display)
[Ruby on Rails] Post image preview function in refile
[Ruby on Rails] Search function (model, method selection formula)
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Rails fuzzy search function implementation
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Rails 6] Implementation of search function
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
[Rails] Implementation of category function
Commentary on partial! --Ruby on Rails
Login function implementation with rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
[Rails] Implementation of tutorial function
Ruby on rails learning record -2020.10.06
[Rails] Implementation of like function
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[Rails 6] Pagination function implementation (kaminari)
[Ruby on Rails] Bookmark (favorite registration, like) function: One direction
[Rails] Create an email sending function with ActionMailer (complete version)
A note about the seed function of Ruby on Rails
[Ruby on Rails] Implement login function by add_token_to_users with API
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
[Rails] Asynchronous implementation of like function
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Rails] Implementation of image preview function
Basic knowledge of Ruby on Rails