Rails Mailer

2 Min. Read
Aug 10, 2019

Introduction

Rails Mailer could be the easiest and the simplest method for sending the mail from the application to the particular user. They inherit from ActionMailer::Base and live in app/mailers, and they have associated views that appear in app/views.

Generating Rails Mailer

Type rails g mailer UserMailer in your terminal.This generates a mailer controller, directory for views, mailer controller, test file , preview file.

1
2
3
4
5
6
7
8
9
10
    $ bin/rails generate mailer UserMailer
    create  app/mailers/user_mailer.rb
    create  app/mailers/application_mailer.rb
    invoke  erb
    create    app/views/user_mailer
    create    app/views/layouts/mailer.text.erb
    create    app/views/layouts/mailer.html.erb
    invoke  test_unit
    create    test/mailers/user_mailer_test.rb
    create    test/mailers/previews/user_mailer_preview.rb

In app/mailers/application_mailer.rb , replace default from: '[email protected]' with your email address.

Assuming the mail is to be sent upon the user sign-up. In app/mailers/user_mailer.rb , add the following method:

1
2
3
4
5
6
7
class UserMailer < ApplicationMailer

 def welcome_email(user)
   @user = user
   mail(to: @user.email, subject: 'Welcome to .....')
 end
end

Now, create the welcome_email.html.erb in app/views/user_mailer/.This file contains the actual body of the email to be send to the recepient.For e.g

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to example.com</h1>
    <p>
      You have successfully signed up to example.com
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

Instead of HTML emails, it is also possible to send a text version.For this in app/views/user_mailer/ , create a file called welcome_email.text.erb Welcome to example.com, You have successfully signed up to example.com, Thanks for joining and have a great day!

In config/environments/development.rb, replace ‘[email protected]’ with you created Gmail address

1
2
3
4
5
6
7
8
9
config.action_mailer.delivery_method = :sendmail
# Defaults to:
# config.action_mailer.sendmail_settings = {
#   location: '/usr/sbin/sendmail',
#   arguments: '-i'
# }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: '[email protected]'}

Then copy and paste this next part, which is specific for Gmail.Replace the following line with the domain of app , gmail username and password

1
2
3
4
5
6
7
8
9
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address:              'smtp.gmail.com',
port:                 587,
domain:               'example.com',
user_name:            '<username>',
password:             '<password>',
authentication:       'plain',
enable_starttls_auto: true  }

Lastly, here is the line of code that will execute the action of sending an email: ruby UserMailer.welcome_email(@user).deliver_now

If the email is to be sent when a user signs up, the code should be placed in a users controller as

1
2
3
4
5
6
7
def create
  @user = User.new(user_params)
  if @user.save
  UserMailer.welcome_email(@user).deliver_now
   ...
  end
end