There was an implementation of the inquiry mail sending function for a certain matter. Personally, I will implement it myself for the first time, so as a memorandum, and since I could not implement it with just one article, I think that it will be useful for other people if I can complete the implementation just by looking at this article.
#Set any model name and required columns
$ rails g model inquiry name:string text:string
$ rails db:migrate
A class that inherits ApplicationMailer required to send an email can be created from the generate command.
$ rails g mailer inquiry
There are two things you need to do to be able to send emails from Rails. ① Password two-step verification setting ② Setting the app password ↑ can be set from here. After setting, please describe the following.
config/enviroments/development.rb
#If true, an error message will be displayed when the email is not sent
config.action_mailer.raise_delivery_errors = true
#Specifies whether fragment caching should be enabled in the mailer template
config.action_mailer.perform_caching = false
#Specify the delivery method
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
 address:              'smtp.gmail.com',
 port:                  587,
 domain:               'gmail.com',
 user_name:            '<gmail email address>',
 password:             'App password',
 authentication:       'plain',
 enable_starttls_auto:  true
}
app/mailer/inquiry_mailer.rb
def send_mail(inquiry)
  @inquiry = inquiry
  mail(
    from: '[email protected]', #Sender email address
    to:   '[email protected]',   #Destination email address
    subject: 'Inquiry email' #subject
  )
end
app/mailer/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  default from: '[email protected]'
  layout 'mailer'
end
To create the layout of the email body, create an erb file according to the naming convention. (app/views/mailer name_mailer/method name of mailer class.text.erb)
app/views/inquiry_mailer/send_mail.text.erb
==================================
<%= @inquiry.name %>I received an inquiry from Mr.
==================================
<Inquiry content>
<%= @inquiry.text %>
As for the class that inherits ActionMailer :: Preview, the InquiryMailerPreview class generated by the generate command exists, so write the following in it and check it.
spec/mailers/previews/inquiry_preview.rb
# Preview all emails at http://localhost:3000/rails/mailers/inquiry
class InquiryPreview < ActionMailer::Preview
  def inquiry
      inquiry = Inquiry.new(name: "Rennosuke Mihara", text: "Inquiry message")
      InquiryMailer.send_mail(inquiry)
  end
  private
  def inquiry_params
    params.require(:inquiry).permit(:name, :text)
  end
end
To check, start the server and connect to http: // localhost: 3000/rails/mailers/inquiry.
Describe the process to be sent to the controller. That way, you'll get an email when you perform that action.
 app/controllers/inquiries_controller.rb
class InquiriesController < ApplicationController
~ Omitted ~
  def create
    @inquiry = Inquiry.new(inquiry_params)
    if @inquiry.save
      #An email will be sent by executing this command
      InquiryMailer.send_mail(@inquiry).deliver
      redirect_to inquiries_path, flash: {success: 'Transmission is complete'}
    else
      flash.now[:alert] = 'Enter the required items, or there is an error in the entered contents'
      render :index
    end
  end
  private
  def inquiry_params
    params.require(:inquiry).permit(:name, :text)
  end
end
You can check it on the console ○
$ rails c
irb(main):001:0> inquiry = Inquiry.new(name: "Sample Taro", text: "Inquiry email")
irb(main):002:0> InquiryMailer.send_mail(inquiry).deliver_now
It's surprisingly easy to implement. It took me a while to realize that I personally set up Gmail, but I think that if you set it up properly, you will not stumble! I think it's a feature I use a lot, so let's implement it! !!
[Introduction to Rails] Explaining Action Mailer email sending from the basics for beginners
[Rails] Email sending settings ~ Using gmail ~
Recommended Posts