Ruby on Rails Action Mailer Configuration
How does Action Mailer work?
Action Mailer overview
Rails has a wonderful built-in mailing system called Action Mailer, which allows us to send email from our application as long as we have a mailer model. Mailers are actually a lot like controllers: they have their own
app/mailers directory, and each mailer has its own associated view as well. Each mailer also inherits from
ActionMailer::Base, and just like controllers, can be generated really easily.
This tutorial explains how to configure Ruby on Rails in the development environment toward sending emails via Ruby on Rails Action Mailer integrated with the most popular email providers Gmail & SendGrid.
Let’s take it step by step, first we need to do a bit of configuration, we are going to send emails from the developement enviroment.
ActionMailer Gmail Confiruation
In order to use Action Mail with Gmail, the best solution that does not violate your other security such as 2-step certification is using App Password
- Open your Account Security Settings
- Navigate to App Password
- On the App Password page, select “Mail” app, and “Other” device, now we provide the name of ruby on Rail application. Then click “Generate”.
- Copy your App password from the yellow Box.
ActionMailer Rails Confiruation
Ruby doesn’t require any additional gems to be installed, as it’s just a simple smtp setting, we just need to configure the setup by adding the following code into our file development.rb so that the Action Mailer and mailcatcher will be setup properly.
for mote informaition refer to Action Mail Confiruation for Gmail.
To start, we’ll use Rails to generate a mailer:
Creating a Mailer
So the first thing we need to do is to create a new Mailer. To do this we can use the Rails generator:
rails generate mailer UserMailer welcome_message
Running this command in the terminal generates a few different files for us. We now have an
app/mailers directory, with an
class ApplicationMailer < ActionMailer::Base
default from: "firstname.lastname@example.org"
application_mailer.rb is the base class that all other mailers are going to inhert from, we notice it inherts from ActionMailer::Base and the first thing it does it cause the default method default from: "email@example.com" and passes some options, these option will be applied to all of our email by default because it's defined up here in the base class. The from option is the email address you want to use as the sender of the email.
class UserMailer < ApplicationMailer
@greeting = "Hi"
mail to: "firstname.lastname@example.org", subject: 'Account registration'
It also generated this UserMailer, notice that the user_mailer.rb inherts from the ApplicationMailer.
Now this mailer classes works similarlly to rails controllers, they inherts from the common base class, in this case “ApplicationMailer”. Thet also has a method inside of him, notice that this method correspond to the name we gave in the command line “welcome_message” , and those methods are similiar to controller actions, but instead of generating an HTML response, “mailer” method generate an “email”.
So this welcome_message is goning to generate the email we want. we also see that step down method includes this “@greeting” instance variable, an instance variables define inside of welcome_message method are accessabile in view templates just like we would expect with the controller action.
And the last line of these mailer methods must call this mail method to accualty create a mail message a return it. now you notice we can also pass option to this method, in this case we have the option to:, this the person we want to send the email to.
To create that email, the mail method is goning to render some email template, now the generator went ahead and created 2 email templates for us to render that new message. and those templates are over in:
notice that the name of the file includes the name of the mailer method, welcome_message.html.erb that generates an HTML email and welcome_message.text.erb that generates a plain text email.
If we take look at the HTML template, what we see it looks just like any other view template.
It’s a good practice to send both plain text and HTML email and let the email client decide which one to display, and the action mailer does all the heavy lifting for us because we have 2 views templates with the same name but different content types ebedded in their file names, both templates will get rendered and sent out in one multi-part email.
Let’s go ahead and setup a user login with our welcome_message method, so our users_controller.rb looks like:
@user = User.new(signup_params)
session[:user_id] = @user.id
render json: @user, status: :created
It would be handy to change our template so that it generates an email content we want.
<h1>Welcome to www.example.com, <%= @user.name %></h1>
<p>You have successfully signed up to www.example.com</p>
<p>Your email address is : <%= @user.email%></p>
<p>Your user name is : <%= @user.user_name%></p>
<p>Thanks for joining and have a great day!</p>
It doesn’t really matter if you are building a consumer or business application, you’re going to need to send emails to your users as some point.
Thank you for reading! ❤️