-
Notifications
You must be signed in to change notification settings - Fork 47
Getting Started
Getting started with Postmark is very easy. In order to start using the full potential of Postmark Rails Gem, you will need to:
- Signup for a new account
- Retrieve a Server API token, from one of the servers you created in your account
- Retrieve an Account API token (optional)
- Create a sender signature
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 email with Postmark is super easy, steps are the following.
Add postmark-rails
to your Gemfile and run bundle install
.
gem 'postmark-rails'
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 }
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 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
For more advanced examples, we suggest visiting Postmark Ruby Gem wiki pages.
For additional information about the capabilities of the Postmark API, see Postmark Developers Documentation.