I implemented an inquiry form using Gmail in Rails.
First, set up the routing. It is assumed that when you enter the inquiry content on the inquiry form page and send it, you will be taken to the transmission completion page.
config/routes.rb
Rails.application.routes.draw do
  resource :contacts, only: [:new, :create] do
    get "/thanks" => "contacts#thanks"
  end
end
The contents to be entered in the inquiry form are three points: name, email address, and inquiry contents.
$ rails g model contact name:string email:string content:text
$ rails db:migrate
$ rails g controller contacts
app/controller/contacts_controller.rb
class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end
  def create
    @contact = Contact.new(contact_params)
    if @contact.save
      ContactMailer.contact_mail(@contact).deliver
      redirect_to thanks_contacts_path
    else
      render :new
    end
  end
  def thanks
  end
  private
  def contact_params
    params.require(:contact).permit(:name, :email, :content)
  end
end
There are some hiragana parts that match the taste of the site I created, but don't worry.
app/view/contacts/new.html.erb
<h3>Contact Us</h3>
<%= form_with(model: @contact, local: true) do |f| %>
  <%= f.label :name, "name" %>
  <%= f.text_field :name %>
  <%= f.label :email, "mail address" %>
  <%= f.email_field :email, autocomplete: "email" %>
  <%= f.label :content, "Not like" %>
  <%= f.text_field :content %>
  <%= f.submit "Come" %>
<% end %>
app/view/contacts/thanks.html.erb
<h3>Thank you for your inquiry.</h3>
$ rails g mailer ContactMailer
app/model/contact_mailer.rb
class ContactMailer < ApplicationMailer
  def contact_mail(contact)
    @contact = contact
    mail to:"My email address", subject: "Contact Us"
  end
end
app/view/contact_mailer/contact_mail.html.erb
<p>User name:<%= @contact.name %></p>
<p>mail address:<%= @contact.email %></p>
<p>Content of inquiry:<%= @contact.content %></p>
config/environments/development.rb
Rails.application.configure do
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    domain: 'gmail.com',
    port: 587,
    user_name: 'My email address',
    password: ENV["GMAIL_KEY"],
    authentication: 'plain',
    enable_starttls_auto: true
  }
end
I'll put the password in my .env file so that it doesn't go up on GitHub.
Describe the following in the .env file (created if it does not exist) directly under the application directory.
GMAIL_KEY=password
For the password entered here, access Google Account and enter the generated app password.
Security → Google login field If you have not set up 2-step authentication, first set it up and then generate an app password. Enter the password generated here.
Implementation is complete here.
Now when you send an email from the inquiry form

I was able to send and receive safely.
Recommended Posts