[RAILS] Insert an image in the verification email when authenticating devise email --Notes--

Premise

device login function [Rails] Try to introduce devise

Email authentication function [Devise] Email authentication sign-up / in / out function I want to implement email authentication with devise [Rails]

The above two functions have been implemented.

Override mailer and create method

Create "mailer.rb" in "app/controllers/users". Inherit the mailer of device.

mailer.rb


class Users::Mailer < Devise::Mailer
end

Set "config/initializers/devise.rb"

devise.rb


config.mailer = 'Users::Mailer'

Put the image you want to insert in "/ app/assets/images/mailer /". Add a method to insert in HTML format mail to "mailer.rb" created earlier.

mailer.rb


class Users::Mailer < Devise::Mailer
  before_action :add_image

  private

  def add_image
    images = ['***.png', '***.jpg']

    images.each do |img|
      attachments.inline[img] = File.read("#{Rails.root}/app/assets/images/mailer/" + img)
    end
  end
end

HTML editing

Modify the file [/app/views/users/mailer/confirmation_instructions.html.erb "

ruby:confirmation_instructions.html.erb


<p>Welcome <%= @email %>!</p>

<p>Thank you for registering as a member.</p>
<%= image_tag attachments['***.png'].url, alt: "***" %>
<%= image_tag attachments['***.jpg'].url, alt: "***" %>
<p><%= link_to 'Account activation', confirmation_url(@resource, confirmation_token: @token) %></p>

The image is inserted when you register as a member here.

that's all. Thank you for reading this far.

reference

Customize email templates (Mailer view) such as password reset in Rails Devise (https://easyramble.com/customize-mail-template-of-devise.html)

How to customize Devise authentication email

Post images by HTML email [rails] [Beginner]

Production environment [Rails] Email sending settings ~ Using gmail ~

Recommended Posts

Insert an image in the verification email when authenticating devise email --Notes--
Add an item when logging in with devise
[Rails] How to display an image in the view
JavaFX-Load Image in the background
How to output the value when there is an array in the array
I sent an email in Java
Organized notes in the head (Java-Exceptions)
I want to display an error message when registering in the database
[Swift] How to set an image in the background without using UIImageView.