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
[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
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
Build a NAS with DLNA function at the speed of a second with Raspberry Pi and Docker Compose
[Spring Data JPA] Custom ID is assigned in a unique sequence at the time of registration.
[Rails] Implementation of new registration function in wizard format using devise
Check the actual date and time at parse with Java's SimpleDateFormat
[Rails] About Uglifier :: Error: Unexpected token: at the time of deployment
[Illustration] Finding the sum of coins with a recursive function [Ruby]