Sailthru Mailer

An adapter that allows the usage of ActionMailer 3 syntax with Sailthru’s Ruby Client Library

Installation

gem install sailthru_mailer
rails g sailthru_mailer

This will generate an initializer in which you can put your api_key and api_secret. E.g.

# Rails.root/config/initializers/sailthru_mailer.rb
SailthruMailer.settings = {
  :api_key => "MYKEY",
  :api_secret => "MYSECRET",
  :api_url => "https://api.sailthru.com"
}

Setting Defaults

SailthruMailer supports the per-mailer configuration that ActionMailer provides.

class MyMailer < SailthruMailer::Base
  defaults(:from => "[email protected]", :reply_to => "[email protected]")
end

We also provide a (nicer) interface within a defaults block

class MyMailer < SailthruMailer::Base
  defaults do
    from("[email protected]")
    reply_to("[email protected]")
  end
end

Or even outside of the defaults block (the block is just syntactic sugar)

class MyMailer < SailthruMailer::Base
  from("[email protected]")
  reply_to("[email protected]")
end

Usage

This gem is designed as a drop-in replacement for ActionMailer. You should be able to just replace all instances of ActionMailer::Base with SailthruMailer::Base

Under the Hood

There are some differences between the way ActionMailer and Sailthru work. ActionMailer provides use a Mail::Message, which is basically just an email (headers and body) that has been through AbstractController’s rendering engine.

Examples

Sailthru has us place templates within their system and provide a template name, email address(es) and variables that go within the email. We account for this by allowing you to name each method after a template and supply the variables within the body hash. #to_hash is called on any objects in the body

# Rails.root/app/models/user.rb
class User < ActiveRecord::Base
  # attributes: first_name, last_name, email
end

# Rails.root/app/mailers/my_mailer.rb
class MyMailer < SailthruMailer::Base
  def sailthru_template_name(user)
    to(user.email) # aliased as recipients(user.email)
    body[:user] = user
  end
end

MyMailer.sailthru_template_name(User.first).deliver

This will send a request to Sailthru:

sailthru.send(:sailthru_template_name, "[email protected]", {:user => {:first_name => "Dan", :last_name => "Langevin", :email => "[email protected]"}}, {} #additional options, Time.now.utc.to_s #send date)

Contributing to Sailthru Mailer

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Dan Langevin. See LICENSE.txt for further details.