Send an email with Rails Action Mailer

Uriah Harston
4 min readFeb 2, 2021
Photo by Joanna Kosinska on Unsplash

So you want to unlock the full potential of a Rails website? Do you want to take it from your simple localhost environment to the real world? If you are doing this, then congratulations are in order because you are about to have a lot more fun with Rails.

In a real-world website, it is common to have the capability to send an email for welcoming purposes, authentication purposes, or regular newsletters. Let’s talk about how we can utilize Action Mailer to accomplish this.

The Objective

A couple of months ago, I made a mock website of a company I was interested in. The main focus was CSS, Bootstrap Grid, and Responsive design. Once I finished that aspect of the project, I was looking for what other small things I could mimic on their website, while still being practical. I decided on the subscriber email list. It looks like this.

The goal: Save the email to the Subscriber Model and send a welcome email.

The Model

☝ First things first. Let's generate a Subscriber Model and a matching table in the database.

rails g model Subscriber email:string

If you are using Postgre, your db schema should look tres simple like this

You now have a Subscriber Model as well. Let's go ahead and add some good ‘ol validations for presence, uniqueness, and length. We don’t have time for any funny business. 🤡

The View

You need to set up a view for where the form goes. ActionMailer is an advanced topic, so we will assume you will know how to do that.

Set up a simple form_for with an email field and a submit button. This syntax is not standard ERB. It is Haml, and if you are not using it, this article I wrote explains why you should.

The Controller

For this form_forto work, we need to make a new subscriber instance available to the view where the form resides. So in the controller for this view, perhaps the Subscriber controller:

Also in the Subscriber controller, you will need a create action that will handle the form submission by creating a subscriber by email, and sending a welcome email. (Don’t forget to configure the routes.rb as well)

At this point, this line of code is probably new to you.

We are getting close to making this actually work, but first we need to set up the mailer.

The Mailer

Let us create a mailer for the subscribers to our newsletter.

rails g mailer SubscriberMailer

You should now have a SubscriberMailer. You can now set up methods or actions that will signify individual emails. Here we have set up a welcome_email that will go to all new emails when they are created.

Let’s reiterate this line of code in our controller:

This line is making the newly created subscriber instance available to the welcome_email action as params inside the SubscriberMailer and it is set to deliver right away.

Inside the welcome_email action, you are to specify the to: and the subject: . The body of the email can be created inside the views/subscriber_mailer/home.html.erb.

Configure STMP

ActionMailer will need to login to a real email in order to send messages. This configuration should go into your environments files. Be sure to change to host address accordingly. Here is a Gmail example.

The username would be your Gmail username, but the password is not your usual account password. You will need to generate a new password that will be associated with this app.

  1. Go to your google account at account.google.com
  2. Click on security
  3. Under Signing into Google click app passwords
  4. Create a new app under Custom with a unique name and generate your password

Conclusion

Now go give it a test run and see if you are able to send yourself an email. If you are able to get it to work then celebrate! If it doesn’t quite work then don’t despair! You are very close. Always remember that documentation is king.

Happy Coding!🏆

Sources: https://guides.rubyonrails.org/action_mailer_basics.html

--

--