Ruby on Rails Email automatic sending function implementation

Contents

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

Implementation

① 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
 #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

③ 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"

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

④ 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"

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.

⑥ Edit controller

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

⑥ Edit routing

Finally, set the routing and you're done.

config/routes.rb



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

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