Rails Bindings for Apostle.io
Add this line to your application's Gemfile:
gem 'apostle-rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install apostle-rails
First, generate the gem's initializer:
rails generate apostle:install
This will create the following initializer:
config/initializers/apostle.rb
Apostle.configure do |config|
config.domain_key = 'APOSTLE_DOMAIN_KEY'
end
apostle-rails
is designed to feel like the ActionMailer
API as much as possible.
Changing an existing mailer is easy.
class MyMailer < ActionMailer::Base
def my_mail name, email, message
@message, @name = message, name
mail to: email, subject: "Your message"
end
end
becomes
class MyMailer < ActionMailer::Base
include Apostle::Mailer
def my_mail name, email, message
@message, @name = message, name
mail "my_mail", email: email
end
end
The first param passed to mail
is the template slug, and instead of to
, use email
. Apostle::Mailer
automatically adds any instance variables you set to the Apostle::Mail
instance.
Instead of returning a Mail::Message
object when you call MyMailer.my_mail
you get an instance of Apostle::Mail
, which you can then call deliver
on.
MyMailer.my_mail("Mal Curtis", "mal@mal.co.nz", "Hi there").deliver!
Any instance variables you assign will be converted to their JSON
representation via #as_json
. This can end up adding extra information which you may not want to send, so it might be easier to create your own hash representations of information.
def new_book book, email
@book = { title: book.title, author: book.author.name }
mail "new_book", email: email
end
Created with ♥ by Mal Curtis (@snikchnz)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request