The RailsNotes Newsletter 🟥 ISSUE #6

🟥 ISSUE #6 (ActionMailer, RailsNotesUI, letter_opener and MailHog)

This week, I’ve been experimenting with MidJourney 😎 How would you like this dude to deliver your mail instead of ActionMailer? — “red cyberpunk postman robot delivering a bundle of mail, warm lighting, nature, by Ralph Bakshi“

Welcome to The RailsNotes Newsletter — Issue #6!

This week we passed 400 subscribers! 🎉🥳

As always, I want to say a big hello 👋 to all the new people. It’s always great to see new people subscribing, and it’s been great to chat with a few of you who replied to the welcome email!

This issue is all about ActionMailer and sending emails 📬.

Why ActionMailer, I hear you ask? It’s because today, I have a very special announcement

📣 SPECIAL ANNOUCEMENT 📣

And to say thank you for reading my newsletter, I've got a discount code for you 🤑

Building ActionMailer templates with HTML tables sucks.

With these components, you won’t ever have to code a <table></table> again! Instead, you can craft your email templates with prebuilt ViewComponents like Email::Button.

You can use the code NEWSLETTER10 to get an extra 10% off your pre-order (this code will expire when the next issue of the newsletter goes out, so don’t sleep on it!).

Pre-order the RailsNotesUI ActionMailer components now and you’ll get a special pre-order discount, plus a very nice thank-you email from me 😎 

FEATURED ARTICLE —

This… is not the MailHog from my article 🐷 (But he might be the one from your nightmares tonight 🙉)

I had no idea the world of email previewing in Rails was so diverse! This week I tested 4 (!!!) great tools, all with their own pros and cons. In this article, I go deep into each one, show you how to use them, and compare them.

The 4 great tools I looked into this week were —

What do I mean by email previewing? 

If you’re sending emails from your Rails apps, you need a way to develop and preview these emails. All of the tools let you preview your emails in your browser for easy testing.

ActionMailer includes a native way to preview mailers by creating ActionMailer::Preview instances, which will render your emails directly in the browser (without actually sending them).

The other 3 options all hook into the ActionMailer delivery method, and capture the emails your Rails app sends.

If you’ve ever had to deal with emails in your Rails app, I think you’ll find this article really interesting!

🌐 MORE ARTICLES —

This might be the best ActionMailer article on the entire internet. Every BoringRails article is packed with great info, and this one is no different. This article starts with a couple of great tips (super handy in their own right), but then goes deep into “Parameterized mailers”.

This style of writing mailers was introduced in Rails 5.1, and lets you write code like MyMailer.with(user: user), passing in the user as a parameter to a mailer. There’s a good chance that this article will change how you write your mailers (and that’s probably a good thing!).

I don’t normally include more than 1 RailsNotes article in each newsletter, but I think this article fits well here. In this article, I walk you through a refactoring I did in a Rails app, to replace Sendgrid API calls with ActionMailer.

I think it’s a great way to wet your feet with ActionMailer if you’ve never used it before, and it might give you some ideas for refactorings you can do in your own Rails apps.

This tweet was a major inspiration for my ActionMailer Components. Matt Swanson is the author of boringrails.com, from the first article, and his Twitter profile is nearly as good as his blog — he’s always sharing great Rails tips and tacit knowledge that I doubt you can find elsewhere.

This tweet is about him adding component-based emails to his Rails app, and once I saw it, I couldn’t stop thinking about it!

— ⚒️ HANDY TIP — 

→ Address your Rails emails nicely using email_address_with_name

Using email_address_with_name in your mailers is a great way to add a bit more personality to them.

Rather than your user receiving an email addressed to [email protected], it’ll appear in their email client as User's Name ([email protected]).

Nothing earth-shattering, but it’s a nice touch, and really easy to implement (I actually borrowed this tip from the BoringRails article above. Highly recommend reading it!)

Here’s an example —

# By default your email will be addressed like — 
#
# To: <[email protected]> *
# Subject: hello?

# Using email_address_with_name, we can do — 
#
# To: User <[email protected]> *
# Subject: hello?

---------------------

# Default
#
def test_email()
  mail(
      to: "[email protected]", 
      subject: "hello?"
     )
end

# email_address_with_name
#
def test_email()
  mail(
      to: email_address_with_name("[email protected]", "User"),
      subject: "hello?"
    )
end