Sending Email with Rails

Jan 28, 2021 • posted by Michael Hartl

Michael Hartl here from the Rails Tutorial and Learn Enough.

One of the topics covered by the Ruby on Rails Tutorial is sending email with Rails. It can be difficult to find up-to-date resources on this subject, which is part of why I added a couple of chapters on it starting in the 4th edition of the tutorial.

For reasons described below, I’ve now updated the main chapter on sending email to make it 100% free:

images/figures/account_activation

The current service covered by the tutorial for sending emails in production is SendGrid. SendGrid has a generous free tier and generally works well for the purposes of the Rails Tutorial. There are two caveats, though:

  1. SendGrid recently changed how they handle authentication, rendering the treatment of the subject in the print edition1 obsolete.
  2. I’ve had numerous reports of SendGrid accounts being immediately frozen, with long reply times from customer service. I’ve intervened repeatedly with SendGrid customer support on behalf of Rails Tutorial readers, and have also filed a ticket to find a more permanent solution. My hope is that we can find a way to preapprove Rails Tutorial–affiliated accounts, but the situation as it stands is unsustainable, and I might have to swap out SendGrid at some point. UPDATE: Based on a tip from a helpful reader and some googling around, I discovered SendGrid’s Single Sender Verification page, which explains how to add a verified Single Sender that I hope will prevent the problem described. I have updated the Ruby on Rails Tutorial accordingly.

Releasing Chapter 11 for free solves the first problem since the online version uses the latest SendGrid authentication technique (specifically, an API key in place of the previous username/password authentication).

images/figures/sendgrid_create_api_key

You can consider Chapter 11 to be a general reference on sending email in production since even those not following the Ruby on Rails Tutorial can clone the sample app and check out the corresponding branch:

$ git clone https://github.com/mhartl/sample_app_6th_ed
$ git checkout account-activation

(See the sample app README for more information.)

In addition, if I end up having to swap out SendGrid for another email service, I can now simply update the freely available chapter with the new code. (I already have a working prototype using MailGun as an alternative.) Should that become necessary, I’ll plan to update this post with a note to that effect.

Please enjoy Chapter 11: Account activation and especially Section 11.4: Email in production as a free reference on sending email with Rails!

1. This is an affiliate link. As an Amazon Associate, I earn from qualifying purchases.
MORE ARTICLES LIKE THIS:
learnenough-news , rails , email