How to use Action Mailer to send emails in Rails.

You are now familiar with the Rails application now. You understand how the MVC works. You can build a website with the interface using Rails view. Now you want to learn some amazing libraries in Rails that make your website look more professional. In this blog, we will together learn how to use Action Mailer to send notifications to user’s emails.

Action Mailer allows you to send emails from our application using mailer classes and views. For the purpose of this article, we will try to send a welcome email to the user who created successfully their account on our website.

I assume that your Rails app would have the function to create a new account and a view page for that. Like below:

CREATE MAILER ACTION

Now we will create a mailer class for user model by run this line in your terminal of the root directory.

rails g mailer user

It will create a “user_mailer.rb” file inside the app/mailers folder and “user_mailer” folder inside the app/view folder.

Inside user_mailer.rb, we will create a method that contains the action to send a welcome email. The action needs the user's email address and the subject of the email.

The UserMailer class is inherited from ApplicationMailer class. Go to application_mailer.rb file (same folder with user_mailer.rb file), then change default email to the email that you will use to send a welcome email to new users.

We create an instance variable “@user” inside the method because we can access that variable when creating the view template.

Now go to the “user_mailer” folder inside app/view folder and create a view file with the same name as the method name inside UserMailer class.

For example, we created the notify_user method above, then the new view file should be notify_user_html.erb. Inside this file, you can design a mail template that you want to send with your email to new users.

SET UP ENVIRONMENT

You want to send a welcome email from your email to users’ email. But the Rails app doesn’t know the password of your email, right? Imagine like when you want to send an email to others, you need to have your email and password to login to the email platform (Gmail, Outlook….).

We need a way to hide our password inside our Rails app.

Before that, we need to config our development file. Go to config/environment/development.rb file, add this configuration to that file.

config.action_mailer.delivery_method = :smtpconfig.action_mailer.smtp_settings = { address:              'smtp.gmail.com', port:                 587, domain:               'example.com', user_name:            ENV['your_email'], password:             ENV['your_password'], authentication:       'plain', enable_starttls_auto: true }

We will use Figaro gem to help hide our environment variable. Run this command in your terminal.

figaro install

This creates a commented config/application.yml file and adds it to your .gitignore. Add your email and password to this file and you’re done.

CALL MAILER ACTION

Now we set up to call the sending email action whenever a new user created. Go to the method inside a controller that you write a create action. It often the create method inside user_controllers. We call the UserMailer class after a new user saved it in our system (We set up UserMailer class in STEP 1. It looks like below:

Perfect! Now whenever someone creates an account on your website/application, a welcome email will send automatically.

Happy Coding!

Full Stack Developer Connect with me here: https://www.linkedin.com/in/chi-nguyen-swe/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store