Quickstart Billing Subscriptions Deployment Kamal

How to send emails in Rails with Postmark

This article shows how to set up Action Mailer and Postmark to deliver transactional emails. We’ll cover configuring desired sender signatures and adjusting Rails production configuration.

Email address

To send an email you’ll need an email address. A lot of domain registrars offer a basic email hosting if you don’t know how to get one.

Remember that Postmark requires a custom domain for email delivery, so your GMail address won’t work.

Once you have it, set up an account in Postmark and create your first server.

Streams

Postmark splits your email into three different streams. The one to configure regular emails sent to your users is called Transactional Stream. In Postmark, choose your server, and go to Message Streams -> Default Transactial Stream.

Here you can find steps for configuring Rails or DKIM verification for your transactional email. The steps will also include your API token which you can also find under API Tokens menu. You’ll need the token later.

Action Mailer

Postmark comes with its own postmark-rails gem for streamlining the Action Mailer configuration in Rails.

We’ll need to add it to a Gemfile:

# Gemfile
gem 'postmark-rails'

And run bundle to install it:

$ bundle

Once done, all we need to do is to pass the Postmark API token:

# config/environments/production.rb
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :postmark

config.action_mailer.postmark_settings = {
  api_token: ENV["POSTMARK_API_TOKEN"]
}

# or use Rails Encrypted Credentials

config.action_mailer.postmark_settings = {
  api_token: Rails.application.credentials.postmark_api_token
}

To set the production credential you can run the following command:

$ SECRET_KEY_BASE="" rails credentials:edit --environment production

Note: Use your existing SECRET_KEY_BASE.

Signatures

Sender signatures are the FROM: email addresses like notifications@upbeatbot.com.

To use them in Rails, set them in your mailers as from:

class TestMailer < ApplicationMailer
  default from: 'strzibny@strzibny.name'
  ..

You can confirm predefined signatures in Postmark or ideally set up DKIM verification to be able to use any address on your domain name. It’s a simple DNS change and Postmark will tell you exactly what to do.

Note: If you configure a Postmark signature with a name it will be automatically appended, so you can just use pure addresses when referencing them in Rails.

Devise

Since Business Class depends on Devise, we need to tell Devise about the right sender signature too:

# config/initializers/devise.rb
Devise.setup do |config|
  ...

  # ==> Mailer Configuration
  # Configure the e-mail address which will be shown in Devise::Mailer,
  # note that it will be overwritten if you use your own mailer class
  # with default "from" parameter.
  config.mailer_sender = "test@businessclasskit.com"

This address will be used for account notifications such as email confirmation or password reset.

Your emails will contain links to your brand new web application, so don’t forget to tell Action Mailer about your hostname:

# config/environments/production.rb
config.default_url_options = {host: "deploymentfromscratch.com"}

Conclusion

Sending emails from Rails with Postmark can be done using the official gem and little bit of configuration. Remember that before going live, you’ll still need to request an approval from Postmark. And if you want to further improve delivery, consider getting a private IP address.