How I send emails with Rails 5 + Postmark

In this article, I’ll explain how I set up my Heroku-hosted Rails 5 applications to send transactional emails with Postmark. Like most of Rails, it’s extremely easy and only takes a few minutes.

Why Postmark is a good choice

Let’s say your app is a success. You’re gaining more users every day. After a few weeks, your users start to complain: they’re not receiving the emails to reset their password, or any notification emails, or even their billing emails. What happens when your email never arrives at scale? You have a bunch of angry, vocal customers, and you need to spend your time doing customer support and fighting fires instead of developing your app. This is why email deliverability is important. I’d rather build a rock-solid, capable system that I can leave alone than a leaky boat that needs to be fixed at random times.

When I was looking into email reliability for my apps, I came across a lot of horror stories. SendGrid and Mailgun supposedly have deliverability problems from time to time, and many smaller options were overly sensitive on banning your account if anyone reported your message as spam. Since I build apps for clients, I also wanted their apps to be reliable – I like to avoid angry client calls in the middle of the night.

Postmark was the exact opposite: rock-solid deliverability, fast, and easy to use. In addition, they have a lovely status page where you can see the actual state of their application. I love the transparency here.

They let you sign up and develop for free: it’s $0 for up to 100 transactional emails per month, and you can send unlimited (free) test emails to accounts on your main domain. In production, it’s $10 for 10,000 emails, and then $50/month for 50,000 emails. If you’re sending upwards of 10,000 billing/forgot password/notification emails, you can probably afford the $50/month price.

Setting up Postmark to send email in Rails 5

  1. Start by signing up at postmarkapp.com.

  2. Verify your email and set up your DNS settings under “Sender Signatures”.

  3. Set up your first server.

  4. Click on “Credentials” in the subnav and copy the Server API token.

  5. Add Postmark to your Gemfile:
    gem 'postmark-rails'
    
  6. Run bundle install.

  7. Add the Postmark API token from Step 4 to config/secrets.yml:
    postmark_api_token: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    
  8. Set Postmark as the delivery method within application.rb. If you’re using Devise, it will plug into this step automatically, so you don’t need to configure anything else to send its emails.
    config.action_mailer.delivery_method = :postmark
    config.action_mailer.postmark_settings = { :api_token => Rails.application.secrets.postmark_api_token }
    
  9. If you want to send additional email (like a welcome email, or notification emails), you’ll need to set up a mailer. You can follow my tutorial on setting it up. It’s as easy as creating a controller. If you’ve already done this, continue to Step 10.

  10. Now you can send email:
    class TestMailer < ApplicationMailer
      def message
        @user = params[:user]
        mail(
            subject: 'It worked!',
            to: @user.email,             # set this to the email you verified with Postmark when testing
            from: 'you@example.com',     # use the email you verified with Postmark
            html_body: '<div>...</div>', # you can delete this; it'll use Rails' HTML/text emails from my tutorial in Step 9
            track_opens: 'true'          # as easy as that
        )
      end
    end
    
  11. If you want to see metrics on the emails you’ve sent, take a look at your overview on the Postmark dashboard.

And just like that, we’ve set up transactional email for our Rails 5 app with Postmark.