Implementing Action Mailer — Ruby on Rails
Receiving automated emails has become so commonplace with modern websites and applications that it almost feels weird not to receive one in some form or fashion. That is why, as a developer, it’s important to understand how to implement a mailer. Luckily for anyone learning Ruby on Rails, or anyone currently working with Rails, Action Mailer is here to help.
What is action mailer?
According to the Ruby on Rails Guides, “Action Mailer allows you to send emails from your application using mailer classes and views”.
Before going any further, if you aren’t familiar with the Model-View-Controller pattern, it might help to take some time to read up on it as Action Mailer relies on the same design patterns.
Implementing Action Mailer in your Rails Project
Step One: Create a Mailer with rails g mailer < YourMailerName >
Running rails g mailer
will create the following files:
% rails g mailer ShippingConfirmation
create app/mailers/shipping_confirmation_mailer.rb
invoke erb
create app/views/shipping_confirmation_mailer
invoke test_unit
create test/mailers/shipping_confirmation_mailer_test.rb
create test/mailers/previews/shipping_confirmation_mailer_preview.rb
Step Two: Create an Action/Method in the Mailer
The mailer acts as a controller, so any instance variables you defined in this email action will be accessible inside of the emails view. This is where the MVC pattern comes into play!
Don’t forget to define the default email address that will be used to send the emails. You can define it inside of the mailer you just created, or inside of the Application Mailer that Rails created for us since our mailer inherits from Application Mailer. Using Application Mailer to store the default email address helps keep our code DRY if you have multiple mailers.
Step Three: Customize the Mailer View
To keep this short and sweet I won’t show this but Rails also generates a .text.erb
view file that is good practice to use in case any of your users prefer not to receive html emails.
Step Four: Calling the Mailer from a Controller
This step is pretty straight forward. Any variables you want to use in the mailer can be passed in as parameters to the .with
method. Whichever action we want to use in our mailer can be called after the .with
method using dot notation.
Action Mailer allows emails to be sent now or later using .deliver_now
or .deliver_later
. The latter takes advantage of Active Job, which allows emails to be sent outside of the request response cycle, think asynchronously, which can make your application feel faster for the user.
Step Five: Configure Rails App to Use Gmail
# config/environments/development.rbconfig.action_mailer.delivery_method = :smtpconfig.action_mailer.default_url_options = {
:host => '<your_url_here>',
:protocol => 'http'
}config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 601,
:user_name => <gmail_username>,
:password => <gmail_password>,
:authentication => 'plain',
:enable_starttls_auto => true
}
If you are running locally you would use localhost:3000 or whichever port you are running on.
Step Six: Configure Gmail to Allow your Rails Application
This last step can be kind of tricky so I recommend going straight to the source. Google provides a great walkthrough of how to give third party apps access to your gmail. Getting this to work requires enabling two-factor authentication and generating an app password.
That’s all you need to get Action Mailer working in your Rails app. There are a lot of other available features and options so I encourage you to take some time to read the Rails Guide linked at the beginning.
Action Mailer, like most of Rails, allows you to do a lot of work without writing too much code. After reading this, I hope you now have the know how necessary to implement this essential feature.
Feel free to leave a comment below with your thoughts on implementing Action Mailer or any feedback you have for me!