The official Transactional Email Ruby gem provides a lightweight wrapper around the Transactional API, and mirrors the structure and functionality of the Transactional API. Note that Transactional Email was formerly known as Mandrill so you're likely to still see the Mandrill naming convention.
The gem assumes your API key is stored as an environment variable called MANDRILL_APIKEY
.
To send a simple HTML message with the messages/send API endpoint, create a new instance of the class, and then pass a single parameter, the message:
require 'mandrill' m = Mandrill::API.new message = { :subject=> "Hello from the Mandrill API", :from_name=> "Your name", :text=>"Hi message, how are you?", :to=>[ { :email=> "recipient@theirdomain.com", :name=> "Recipient1" } ], :html=>"<html><h1>Hi <strong>message</strong>, how are you?</h1></html>", :from_email=>"sender@yourdomain.com" } sending = m.messages.send message puts sending
Rendering a template using templates/render
:
require 'mandrill'
m = Mandrill::API.new
rendered = m.templates.render 'MyTemplate', [{:name => 'main', :content => 'The main content block'}]
puts rendered['html'] # print out the rendered HTML