Embracing whimsy: Sprinkling fairy dust on Rails 7 MailerSend integration

Embracing whimsy: Sprinkling fairy dust on Rails 7 MailerSend integration
Photo by Brett Jordan / Unsplash

Transactional emails are the lifeblood of web applications, delivering crucial information and updates to users. But who said transactional emails have to be boring? In this blog post, we will explore how the powerful combination of Rails Action Mailer and MailerSend can infuse whimsy and enchantment into your transactional email communications. Not really... but you can get up and delivering production transactional emails without getting out your credit card.

My project, kidcarekit, has reached the point where I want to test things like email verification and provide the ability for users to reset their password via email. As it turns out with Rails setting up SMTP is as simple as adding a few lines to your environment files.

In development I'm using the letter_opener gem that automatically pops up the email your app would've sent in a new browser tab. It's insaely powerful and easy to setup. In your gemfile add (under development only so avoid loading this gem in your production environment) like this:

group :development do
  # Don't send emails in development
  gem 'letter_opener'
end
gemfile

You will also need to modify your development.rb file to use your new gem.

  # Config letter opener for development.
  config.action_mailer.delivery_method = :letter_opener
  config.action_mailer.perform_deliveries = true
development.rb

Bundle install and then run your app to the point of an email being generated and you should be presented with your test email in a new browser tab. NEAT!

Example of letter_opener gem showing a transaction email in my SaaS dev environment
💰
Affiliate link for MailerSend below

Now for your production emails. I'm using the free tier of MailerSend via SMTP which is super easy to configure for your Rails app. As of writing this post they have a super generous free tier; 3000 emails a month then 1$ for each additional thousand emails your app generates.

I will leave setting up and verifying your account with MailerSend an exercise to the reader, but once you have it is simple to configure Rails via the production.rb environment configuration to use the SMTP server provided by MailerSend.

  # Set default_url_options host
  config.action_mailer.default_url_options = { host: "www.kidcarekit.com" }
  config.action_mailer.delivery_method = :smtp

  # SMTP settings for Mailerlite
  config.action_mailer.smtp_settings = {
  :address => ENV["MAILERSEND_SERVER"],
  :port => 587,
  :user_name => ENV["MAILERSEND_USERNAME"],
  :password => ENV["MAILERSEND_PASSWORD"],
  }
production.rb

Mine looks like this. I host my application on fly.io now that Heroku's value proposition has decreased. One of the beautiful things about fly.io is the management of secrets via the environment. I don't recommend storing your actual credentials in your source code.

Thanks for sticking around and happy building!