For sending emails in rails,ActionMailer Class is used. Emails can be sent using any service providers like Gmail, Sendgrid, Mandrill or any other. Here we will see how we can send the emails using our gmail account. The steps are given below: Step 1: Create a mailer in your rails app.
Step 2: Open your app/mailers/notification_mailer.rb and set the default from and also create a method for sending emails
class NotificationMailer < ApplicationMailer
default from: "email@example.com"
mail(to: receiver_email, subject: 'Sample Email')
Step 3: Create a file app/views/notification_mailer/notification_email.html.erb. This file will be served as a body template in the mail. In case you want to use some dynamic content in this template, you can declare them as an instance variable in the notification_email action and use them in the view page
This is a test email
Step 4: Configure the mailer with gmail username, password and smtp inside environment files config/environment/development.rb and config/environment/production.rb
If during sending emails, you get any error like Net::SMTPAuthenticationError, you need to enable less secure apps for sending emails in your gmail setting.
I have also created a sample app for sending emails, clone the repository and run the app locally to test it. Don't forget to place your gmail username and password at config/environment/development.rb . The github url for the app is https://github.com/raghvendra1501/sendemail-using-gmail-rails