Prompt
What's a simple way to send transactional emails from a Ruby on Rails application without adopting a full ESP?
Latest observation
The simplest approach is Action Mailer plus a plain SMTP relay.Comparing SMTP relay options for low-volume sendingComparing SMTP relay options for low-volume sendingThe simplest approach is Action Mailer plus a plain SMTP relay. Rails already ships with everything you need, so there’s no extra gem or ESP SDK required.
- Generate a mailer bashbin/rails generate mailer UserMailer welcome ruby# app/mailers/user_mailer.rb class UserMailer < ApplicationMailer def welcome(user) @user = user mail(to: @user.email, subject: "Welcome!") end end Then write the templates in app/views/user_mailer/welcome.html.erb (and a .text.erb version).
- Point it at an SMTP server ruby# config/environments/production.rb config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: ENV["SMTP_HOST"], port: 587, user_name: Rails.application.credentials.dig(:smtp, :username), password: Rails.application.credentials.dig(:smtp, :password), authentication: :plain, enable_starttls_auto: true } config.action_mailer.default_url_options = { host: "yourapp.com" }
- Send it in the background rubyUserMailer.welcome(user).deliver_later deliver_later uses Active Job, so a slow SMTP server won’t block web requests, and failed deliveries can retry. Where to get the SMTP server
Amazon SES (SMTP interface): very cheap, and you just use it as a relay with no marketing or template features. Needs a bit of setup, including leaving the sandbox. Your hosting or domain provider’s SMTP (Fastmail, Google Workspace, Zoho, etc.): fine for low volume, but watch the daily sending limits. Self-hosted Postfix: doable, but you take on IP reputation and maintenance. I’d avoid it unless you have a reason.
Don’t skip deliverability Whatever relay you pick, set up these DNS records for your sending domain, or your mail will land in spam:
SPF and DKIM (your relay will give you the values) DMARC (start with p=none, then tighten)
Also send from a consistent address on your own domain, include a plain-text part, and keep transactional mail separate from any bulk or marketing mail. Dev tip Use letter_opener or mailcatcher in development so you can preview emails without sending them. If you tell me your expected volume and hosting setup, I can suggest a specific relay and walk through its configuration.just now