Class: Mandrails::Delivery::Mandrill

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrails/delivery/mandrill.rb

Overview

Sending e-mail with Mandrill API

A delivery method implementation which uses the Mandrill REST API. This is done by providing a mailer on top of mandrill-api gem.

Using it with mail gem

Requires the :key option, or set the environment variable MANDRILL_APIKEY to a your Mandrill API key.

Mail.defaults do
  delivery_method Mandrails::Delivery::Mandrill, {
                           :key        => "123...-abcde", # or set the MANDRILL_APIKEY environment variable
                           :from_name  => "Your Name",
                           :from_email => "[email protected]" }
end

Using it with Rails & ActionMailer

Using the railtie the :mandrill delivery method is automatically available, also ensure to set the API key using either :key setting or MANDRILL_APIKEY environment variable. Add something like to the config/environments/*:

config.action_mailer.delivery_method = :mandrill
config.action_mailer.mandrails_settings = {
  key: "123...-abcde", # or set the MANDRILL_APIKEY environment variable
  from_name: "Your Name",
  from_email: "[email protected]" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = nil) ⇒ Mandrill

:nodoc:



45
46
47
48
49
50
51
52
53
54
# File 'lib/mandrails/delivery/mandrill.rb', line 45

def initialize(values = nil) #:nodoc:
  @settings = {
                track_opens: true,
                track_clicks: false,
                auto_text: true,
                merge: false,
                async: false,
                key: ::ENV['MANDRILL_APIKEY'].presence
              }.merge(values || {})
end

Instance Attribute Details

#settingsObject

Provide read/write access, dunno why write access is required, but seems to be in all deliver_methods from mikel/mail as well



43
44
45
# File 'lib/mandrails/delivery/mandrill.rb', line 43

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/mandrails/delivery/mandrill.rb', line 64

def deliver!(mail)
  # TODO: verify incoming `mail` argument, see https://github.com/mikel/mail/blob/master/lib/mail/check_delivery_params.rb
  builder = Mandrails::MessageBuilder.new mail, settings
  response = mandrill_api.messages.send(builder.as_json, settings[:async])

  # Either return response or instance
  return response if settings[:return_response]
  self
end

#mandrill_apiObject

Public: Access to the Mandrill::API instance used to send messages. It raises an error if no key was given or is present.

Returns Mandrill::API instance.



60
61
62
# File 'lib/mandrails/delivery/mandrill.rb', line 60

def mandrill_api
  @mandrill_api ||= ::Mandrill::API.new(settings[:key].presence)
end