Action Mailer — Making Email with Rails simple

Joe Rollins Greiner
3 min readMay 7, 2018

--

When I first started with Ruby on Rails, I was mostly focused on Controllers — that super simple way to get your website up and running. I knew there were other, cool, helpful directories hiding away in my application, but I wanted to get a working, skeleton of an app going for my knowledge’s sake before diving into any of those.

Well, I did that. It’s not the best app I’ll ever make, but it was a great learning experience! Now, to read into those other directories. I peeked into one, jumped to the documentation. It was Action Mailer.

This is actually cool & good.

Action Mailer is a super simple way to send email via your app. It works almost exactly like a Controller- it inherits from an ApplicationMailer, and your methods here are simply pointing to views, which in this case, are HTML or Plain-Text emails you send out to your users.

Getting started is easy. Really easy. Too easy? It’s a generator called ‘mailer’ (duh), and it generates the same way a controller might. It creates a Mailer (think Controller). It creates a views folder of the same name. It creates a test unit. A preview? Not sure what that is yet, but we’ll get there. Simple!

As always, the documentation Rails provides is great here. Create a method in your Mailer…

Wondering where the from: is? We’ll get to that.

and create an .erb file that corresponds to it in the newly created directory. You’re all set to write emails!

Ok, let’s dive into some of the stuff we’re seeing here.

def welcome_email
@user = params[:user]
@url = 'http://google.com/super-admin-login'
mail(to: @user.email, subject: 'Welcome to Google, or something like that')
end
end

In your #welcome_email, I’m calling an instance variable of a user, a url instance variable that just points to a path (this is unnecessary! Just showing that you can do that), and a #mail method. #mail takes a :to, a :subject, a :from, :cc… all your email headers.

My :from is sitting up top of my Mailer! It’s defaulting to an info@google.com email address.

default from: "info@google.com"

In my corresponding view, you can write an HTML email- which is pretty standard these days.

Oh, this is easy.

Easy, right? It behaves the same way as a View.

Ok, now to send my email. For the email I’ve crafted, I’d like for it to be sent for every new user of Google. We’re just going to call it on #create in our UserController.

A super simple User creation, with a super simple Mailer method.

Perhaps this is all painfully simple, but that’s the point- Rails makes intensive processes easy so I can focus on crafting a good product. There are plenty of good uses for a Mailer, obviously. But some great ideas that came to me as I was setting this up:

  • Confirmations, Appointments, Updates, Cancellations
  • Data reports for your team
  • Emergency Alerts when invalid or incorrect actions take place
  • Internal Milestones!

You did it. There’s a lot more you can with Mailer, but this was a great start!

--

--