Skip to content

Getting Started

Igor Balos edited this page Jul 17, 2024 · 13 revisions

Getting started with Postmark is very easy. In order to start using the full potential of Postmark Rails Gem, you will need to:

Account API token is needed only for admins, for managing account details like domains, signatures, etc. To read more about tokens, check out our developer docs.

Sending your first email

Sending email with Postmark is super easy, steps are the following.

Add postmark-rails to your Gemfile and run bundle install.

gem 'postmark-rails'

Rails 6-7

Save your Postmark Server API Token to config/credentials.yml.enc:

run rails secret, then run rails credentials:edit and add:

postmark_api_token: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Set Postmark as your preferred mail delivery method via config/application.rb:

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

Rails 3-5

Save your Postmark API token to config/secrets.yml.

postmark_api_token: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Set Postmark as your preferred mail delivery method via config/application.rb:

config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_token => Rails.application.secrets.postmark_api_token }

Note: The postmark_settings hash can contain any options supported by Postmark::ApiClient.

After setting it all up, you can send emails like this:

class TestMailer < ActionMailer::Base

  def tagged_message
    mail(
      :subject => 'hello',
      :to      => 'sheldon@bigbangtheory.com',
      :from    => 'leonard@bigbangtheory.com',
      :tag     => 'my-tag',
      :track_opens => 'true'
    )
  end

end

Next, we will check out more advanced sending techniques and other API features. Check out our wiki pages sidebar for details.

In case you plan to use multiple server API tokens

In case you plan to use multiple server API tokens setting preferred mail delivery method via config/application.rb might not be sufficient for your needs. What you could do is send emails with Dynamic Delivery Options.

All you would need to do is provide a server API token inside the mailer action like in the following example:

class TestMailer < ApplicationMailer
  def hello
    mail(
      to: 'alice@example.com',
      subject: 'Hello from Postmark',
      delivery_method_options: {
        api_token: '"your-api-token-xxxx-xxxxxxxxxxxx"'
      }
    )
  end
end

More examples

For more advanced examples, we suggest visiting Postmark Ruby Gem wiki pages.