Embracing whimsy: Sprinkling fairy dust on Rails 7 MailerSend integration
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:
You will also need to modify your development.rb
file to use your new gem.
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!
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.
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!