Skip to content

Using Postmark with Mail library

Igor Balos edited this page Oct 1, 2018 · 6 revisions

You can use Postmark with the mail gem.

gem install mail

Make sure you have a sender signature for every From email address you specify.

To send a Mail::Message via Postmark you’ll need to specify Mail::Postmark as a delivery method for the message:

message = Mail.new do
  # ...
  delivery_method Mail::Postmark, api_token: 'your-server-token'
end

Delivery method accepts all options supported by Postmark::ApiClient documented above. A new instance of Postmark::ApiClient is created every time you deliver a message to preserve thread safety.

If you would prefer to use Postmark as the default delivery method for all Mail::Message instances, you can call Mail.defaults method during the initialization step of your app:

Mail.defaults do
  delivery_method Mail::Postmark, api_token: 'your-server-token'
end

Plain text message

require 'rubygems'
require 'postmark'
require 'mail'
require 'json'

message = Mail.new do
  from            '[email protected]'
  to              'Leonard Hofstadter <[email protected]>'
  subject         'Re: Come on, Sheldon. It will be fun.'
  body            'That\'s what you said about the Green Lantern movie. You' \
                  'were 114 minutes of wrong.'

  delivery_method Mail::Postmark, :api_token => 'your-postmark-api-token'
end

message.deliver
# => #<Mail::Message:70355890541720, Multipart: false, Headers: <From: [email protected]>, <To: [email protected]>, <Message-ID: e439fec0-4c89-475b-b3fc-eb446249a051>, <Subject: Re: Come on, Sheldon. It will be fun.>>