Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

PostageApp Ruby Gem Build Status

This gem is used to integrate Ruby apps with PostageApp service. Personalized high-volume email sending can be offloaded to PostageApp via a simple JSON-based API.

API Documentation


Rails 4.x and newer

Add the postageapp gem to your Gemfile:

gem 'postageapp'

Then from the Rails project's root run:

bundle install
bin/rails generate postageapp --api-key PROJECT_API_KEY

Legacy Versions of Rails

Sinatra / Rack / Others

You'll need to install the gem first:

$ sudo gem install postageapp

The configuration will need to be loaded before executing any API calls:

require 'postageapp'

PostageApp.configure do |config|
  config.api_key = 'PROJECT_API_KEY'

If it's more convenient, setting the POSTAGEAPP_API_KEY environment variable with the appropriate API key will also work.


Here's an example of sending a message using the send_message API call:

request =
    headers: {
      from: '',
      subject: 'Email Subject'
    recipients: '',
    content: {
      'text/plain' => 'text email content',
      'text/html' => 'html email content'
    attachments: {
      'document.pdf' => {
        content_type: application/pdf',
        content: Base64.encode64('/path/to/document.pdf', 'rb').read)

response = request.send

PostageApp::Response object allows you to check the status:

>> response.status
=> 'ok'

Alternatively you may use:

=> false
>> response.ok?
=> true

Response usually comes back with data:

=> { 'message' => { 'id' => '12345' }}

Recipient Override

Sometimes you don't want to send emails to real people in your application. For that there's an ability to override to what address all emails will be delivered. All you need to do is modify configuration block like this (in Rails projects it's usually found in RAILS_ROOT/config/initializers/postageapp.rb):

PostageApp.configure do |config|
  config.api_key = 'PROJECT_API_KEY'

  unless (Rails.env.production?)
    config.recipient_override = ''

ActionMailer Integration

You can quickly convert your existing mailers to use PostageApp service by simply changing class MyMailer < ActionMailer::Base to class MyMailer < PostageApp::Mailer. When using ActionMailer from outside of Rails, this will have to be loaded manually:

require 'postageapp/mailer'

There are custom methods that allow setting of template and variables parts of the API call. They are postageapp_template and postageapp_variables. Examples how they are used are below. For details what they do please see documentation

Please note that deliver method will return PostageApp::Response object. This way you can immediately check the status of the delivery. For example:

response = UserMailer.welcome_email(@user).deliver
# => true

Mailer Base Class

Here's an example of a mailer using the PostageApp::Mailer base class:

require 'postageapp/mailer'

class Notifier < PostageApp::Mailer
  def signup_notification
    attachments[''] ='/path/to/')

    headers['Special-Header'] = 'SpecialValue'

    # PostageApp specific elements:
    postageapp_template 'example_template'
    postageapp_variables 'global_variable' => 'value'

    # You may set api key for a specific mailers
    postageapp_api_key '123456abcde'

    # You can manually specify uid for the message payload.
    # Make sure it's sufficiently unique.
    postageapp_uid Digest::SHA1.hexdigest([, ].to_s)

      from: 'test@test.test',
      subject: 'Test Message',
      to: {
        '' => { 'variable' => 'value' },
        '' => { 'variable' => 'value' }

API of previous ActionMailer is partially supported under Rails 3 environment. Please note that it's not 100% integrated, some methods/syntax will not work. You may still define you mailers in this way (but really shouldn't):

require 'postageapp/mailer'

class Notifier < PostageApp::Mailer
  def signup_notification
    from ''
    subject 'Test Email'

    mail(to: '')


Here's an example of using an interceptor:

class DevelopmentPostageappInterceptor
  def self.delivering_email(postageapp_msg)
    postageapp_msg.arguments["headers"][:subject] =
      "[#{postageapp_msg.arguments["recipients"]}] #{postageapp_msg.arguments["headers"][:subject]}"

    postageapp_msg.arguments["recipients"] = ""

    # Deliveries can be disabled if required
    # postageapp_msg.perform_deliveries = false

Automatic resending in case of failure

For those rare occasions when the API is not reachable or unresponsive, this gem will temporarily store requests and then will attempt to resend them with the next successful connection. In Rails environment it will create a folder: RAILS_ROOT/tmp/postageapp_failed_requests and save all failed requests there. On successful resend file for that request will be deleted.

For projects other than Rails you'll need to tell where the project_root is:

PostageApp.configure do |config|
  config.api_key      = 'PROJECT_API_KEY'
  config.project_root = "/path/to/your/project"


(C) 2011-2019 Scott Tadman, Oleg Khabarov, PostageApp


PostageApp Ruby gem makes sending transactional email from applications easy




No packages published