Introduction
I have recently come across the rails module ActionMailer::Base. ActionMailer allows you send emails from your application to users of the application. This module allows you to send a wide variety of different emails from welcome messages to order confirmations. In this blog, I will go over the basics of setting up ActionMailer::Base in a Ruby on Rails project.
Generating the Mailer
To generate the mailer, run this code in your console:
rails g mailer UserMailer
Files
Running this generator will create a few files:
Mailer Class File
A mailer class file is created in the app/mailers directory. Its name is based on the mailer name you provided. In our example, it will create a file named user_mailer.rb. This file contains the definition of the mailer class.
View Directory
A directory with the same name as the mailer is created inside the app/views directory. In our example, a directory named user_mailer will be created. This directory will contain the email view files associated with the mailer.
Email View Files
Inside the mailer's view directory, two template files are created: action.html.erb and action.text.erb. The action portion of the file name corresponds to the name of the email action you define in the mailer class. For example, if you define a welcome_message action, the generated view files will be welcome_message.html.erb and welcome_message.text.erb. These files define the content and layout of the email.
Optional Files: Layout and Test
Layout files provide a common structure or styling that can be applied to multiple email views. The test files are typically created in the test/mailers directory.
Setting up the Mailer
Inside of the user_mailer.rb file is where you will create the actions for the emails you want sent out. Continuing with our previous example, here is how you would set up the mailer to handle sending a welcome message:
class UserMailer < ActionMailer::Base
default from: 'filleremail@example.com'
def welcome_message(user)
@user = user
mail(to: user.email, subject: 'Welcome!')
end
end
In the above example, the UserMailer class is defined, and it includes a welcome_message action. Inside the action, we set an instance variable @user that can be accessed in the corresponding email view. Inside the mailer class, you can define various email actions by creating methods. Each method corresponds to a specific email action. For example, the welcome_message action sends a welcome email to a user when they sign up.
After setting up the mailer class, you need to create the view for the email. For each email action, you need to create a corresponding email view file. These view files are created in the 'app/views/user_mailer' directory. For our example, you would need to create either a 'welcome_message.html.erb' or 'welome_message.text.erb' depending on your needs. We will use a html file for our example:
<h1>Welcome to Our App, <%= @user.name %>!</h1>
<p>Thank you for signing up. We're excited to have you on board!</p>
This is a very basic example, but it shows how you can interpolate the users name into the email to make it more personal.
Calling the Mailer
You can call the mailer from a model or controller. I will go over how to call the mailer from a controller to follow the example of a new user welcome message. Invoking the mailer from the controller would look something like this:
class UsersController < ApplicationController
def create
@user = User.create(user_params)
if @user.valid?
UserMailer.welcome_message(@user).deliver_later
# rest of controller logic
In this example, when a new user is created and is valid, the UserMailer will be called and pass the user to the mailer. Passing the new user to the mailer allows it to get any information that is needed inside of the email that is to be sent. The '.deliver_later' method tells the controller to continue running without waiting for the email to be sent.
Conclusion
Action Mailer simplifies the process of sending emails in your Rails application, allowing you to keep your users informed and engaged. Whether it's welcome messages, order confirmations, or any other type of email, Action Mailer provides the tools you need to communicate effectively with your users.
In conclusion, Action Mailer is a powerful module in Ruby on Rails that enables you to send emails from your application to users. By following a few simple steps, you can set up the mailer class, generate email view files, and define email actions to handle various types of emails.
Top comments (0)