Email sending function with Action Mailer at the time of new registration

Overview

Use ActionMailer to send a welcome email when you register.

Premise

Implementation of login function using devise.

Installation procedure

1. Settings to use Action Mailer

The sender of the welcome email is Gmail. Description of how to set ActionMailer in config / environments / development.rb.

development.rb


Rails.application.configure do

#---Omission---#

 config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    port:                 587,
    address:              'smtp.gmail.com',
    domain:               'gmail.com',
    user_name:            'Source address',
    password:             'App password',
    authentication:       :plain,
    enable_starttls_auto: true
  }

For the app password, issue the first 16-digit password with 2-step authentication set, and enter it in the app password. I have referred to the following. -[Get password for Google mail application] (https://qiita.com/miriwo/items/833d4189140831c5d039) ・ [How to turn on Google two-step verification] (https://qiita.com/miriwo/items/0331e7241710fb4759fe)

2. Create Mailer class

$ rails g mailer UserNotice

3. Edit Mailer class

app/mailers/user_notice_mailer.rb


class UserNoticeMailer < ApplicationMailer
  def send_signup_email(user)
    @user = user
    mail to: @user.email, subject: "Membership registration is complete."
  end
end

4. Create email body

app/views/user_notice_mailer/send_signup_email.text.erb


Welcome<%= @user.name %>Mr

Thank you for registering for an account.

5. User model editing

app/models/user.rb


#---add to---#

  after_create :send_welcome_mail
 
  def send_welcome_mail
    UserNoticeMailer.send_signup_email(self).deliver
  end

You can use after_create to call a method to send an email after a new User has been created.

You should now receive a welcome email.

Recommended Posts

Email sending function with Action Mailer at the time of new registration
How to implement the email authentication function at the time of user registration
[Rails 6] Change redirect destination at the time of new registration / login by devise
[Rails 6] Implementation of inquiry function using Action Mailer
[JavaScript] I can't get the response body at the time of error with axios (ajax)
Speed comparison at the time of generation at the time of date conversion
Whether to make the server side at the time of system rebuild with Kotlin or Java
[Android] Exit the activity of the transition source at the time of screen transition
[Rails] Create an email sending function with ActionMailer (complete version)
About the behavior at the time of error of Files.copy (pathA, pathB)
[Rails] Get access_token at the time of Twitter authentication with Sorcery and save it in DB
[Spring Data JPA] Custom ID is assigned in a unique sequence at the time of registration.
Check the actual date and time at parse with Java's SimpleDateFormat
Setting to start multiple units at the same time with Vagrant
[Rails 6] Implementation of new registration function by SNS authentication (Facebook, Google)
[Rails] About Uglifier :: Error: Unexpected token: at the time of deployment
[Illustration] Finding the sum of coins with a recursive function [Ruby]