Sending Emails in Rails Application


In this tutorial, we will learn how you can send emails from your rails applications. Sending emails is very important for nearly every web application today, from filling out contact forms to user’s account verification, etc. For this tutorial’s sake, we are sending emails to the user for confirming his account. But make sure you have set up the authentication in the rails app.

What is Action Mailer?

Action Mailer allows you to send emails without installing the specified gem or creating an API. It is very easy to use.

Creating the Mailer

Open the terminal and go to the related app directory and type

rails g mailer user

It will creates some files in your app. You have to open


You will see some code like this

class UserMailer < ApplicationMailer


Make sure the user mailer is inherited with application mailer. Now open the application mailer and type

class ApplicationMailer < ActionMailer::Base
  default from: ""
  layout 'mailer'

Rails action mailer are very similar to the controls. They have methods named “actions”. We can send messages using this action to the client.

Open your user_mailer.rb file and type

class UserMailer < ApplicationMailer
  default from: ''
  def welcome
    @user = params[:user]
    @url  = ''
    mail(to:, subject: 'Welcome to My Awesome Site')

You can see we have defined the welcome action. Whenever the user hits this action it performs the functionalities that we have defined into it.

Now we have to create an HTML file for the email template. Go to the app/views/user_mailer/. Create a file and name it welcome.html.erb

Open the file in code editor and type the following code

<!DOCTYPE html>
    <title> Welcome </titile>
    <h1>Welcome to, <%= %></h1>
      You have successfully signed up to,
      Great Job
    <p>Thanks for joining and have a great day!</p>

Now we have to call the action mailer. For doing that go the user controller where you have defined the create action. Type this code

class UsersController < ApplicationController
def create
  @user =[:user]);
   UserMailer.with(user :@user).welcome.deliver_now;
   redirect_to @user, notice: "Account was created"
   redirect_to, notice: "Invalid Data"

Now you have all setup for sending emails. When you hit the signup button it will display you a message in the terminal but it’s actually not sending it. Because we haven’t configured our email yet. We have to configure our SMTP settings. Open config/environments

Add the following code

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              '',
  port:                 587,
  domain:               '',
  user_name:            '<Enter your user name>',
  password:             '<enter your password>',
  authentication:       'plain',
  enable_starttls_auto: true }

This configuration will send email using your Gmail account. That’s it, for now, I hope this will help you in learning purpose. If you have a question write it down in comment box below.

Leave a Reply

Your email address will not be published. Required fields are marked *