DEV Community

Tuyen Ha
Tuyen Ha

Posted on

Getting Started with Action Mailer and GMAIL Configuration

What Is Action Mailer?

Action mailer is a component of the Ruby on Rails framework that makes sending emails from your application a simple process. It provides a framework for creating, sending, and managing email messages.

So let's get started!

The first step is to generate and set up the mailer:

% rails generate mailer WelcomeMail
Enter fullscreen mode Exit fullscreen mode

This will generate the following outputs:

create app/mailers/welcome_mail_mailer.rb
invoke erb
create app/views/welcome_mail_mailer
Enter fullscreen mode Exit fullscreen mode

Inside the # app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
   default from: "from@gmail.com"
   layout "mailer"
end
Enter fullscreen mode Exit fullscreen mode

This code defines a class called "ApplicationMailer" that inherits from the "ActionMailer::Base" class. Inside the class, the first configuration is "default from: 'from@gmail.com", which sets the default sender address. The second configuration is "layout 'mailer'", which sets the default layout template for the emails.

Be sure to replace the from@gmail.com with your chosen default email.

Inside the # app/mailers/welcome_email.rb

Class WelcomeMailer < ApplicationMailer
   default from: 'from@gmail.com'

   def welcome_email
      @user = params[:user]
      mail(to: @user.email, subject: "Welcome to 
      Random Posts")
   end
end
Enter fullscreen mode Exit fullscreen mode

This code defines a class called "WelcomeMailer" that inherits from the "ApplicationMailer" class. Similar to the Controller we are already used to, we can create an email by adding a method to the mailer.

The configuration "default from: 'from@gmail.com'" sets the default sender email address for all the emails sent from the mailer class.

Then there is a method called "welcome_email" which is responsible for sending the physical welcome email.

CREATING THE MAILER VIEWS

Inside the # app/views/welcome_mailer/welcome_email.html.erb

<html>
   <head>
      <meta content='text/html; chatset=UTF=8' http-equiv='Content-Type' />
   </head>
   <body>
      <h1>Welcome to Random Posts, <%= @user.name %</h1>
      <p>You've signed up for Random Posts!</p>
      <p>Thanks for joining us!</p>
   </body>
</html>
Enter fullscreen mode Exit fullscreen mode

This code is a basic ruby HTML file that creates the body of the email in an HTML structure.

Inside the # app/views/welcome_mailer/welcome_email.text.erb

Welcome to Random Posts, <%= @user.name %>
=============================================

You've signed up for Random Posts.

thanks for joining us!
Enter fullscreen mode Exit fullscreen mode

Then we have the body of the email in text format.

CALLING THE MAILER

Inside # app/controller/users_controller.rb

We are going to instruct the WelcomeMailer to deliver a welcome email to anyone who signs up for an account!

class UsersController < ApplicationController
   def create
      @user = User.new(user_params)
      if @user.save
         WelcomeMailer.with(user: @user).welcome_email.deliver_now
         session[:user_id] = @user.id
         render json: @user, status: 201
      else
         render json: {errors: @user.errors.full_messages}, status: :unprocessable_entity
      end
   end
end
Enter fullscreen mode Exit fullscreen mode

The line WelcomeMailer.with(user: @user).welcome_email.deliver_now sends a welcome email using a mailer class named "WelcomeMailer." It passes the "@user" object to the mailer's "with" method. This method prepares the email, then calls the "welcome_email" method to generate the email content. Which then will send out the email when a new user has been created!

For this specific tutorial, I will be using a GMAIL account as the default sender email, and with that we'll continue on with the GMAIL configuration.

GMAIL CONFIGURATION

Inside # config/environments/development.rb,

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address:              'smtp.gmail.com',
port:                 587,
domain:               'example.com',
user_name:            'from@gmail.com',
password:             '**App Password**',
authentication:       'plain',
enable_starttls_auto: true }
Enter fullscreen mode Exit fullscreen mode

By adding the following codes into our file development.rb, we have successfully configured and set up the Action Mailer and Mailcatcher.

The code above is specifically for GMAIL Configuration.

Keep in mind that in order to use Action Mailer with Gmail, an App Password is required. This prevents the violation of your other security such as the 2-step verification.

You can get an App Password by signing into your Google Account => security => and under Signing in to Google.

  1. Turn on your 2-Step Verification if it's not already in use.
  2. Navigate to App Passwords
  3. Select Other (Custom name) and give it a name.
  4. GENERATE
  5. Copy your App Password from the yellow box.
  6. Replace the password with your App Password.

Just like that, you've successfully sent a welcome message!

Top comments (0)