class MandrillMailer::CoreMailer

Attributes

defaults[W]
async[RW]

Public: Enable background sending mode

ip_pool[RW]

Public: Name of the dedicated IP pool that should be used to send the message

message[RW]

Public: Other information on the message to send

send_at[RW]

Public: When message should be sent

Public Class Methods

default(args) click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 154
def self.default(args)
  @defaults ||= {}
  @defaults[:from] ||= 'example@email.com'
  @defaults[:merge_vars] ||= {}
  @defaults.merge!(args)
end
defaults() click to toggle source

Public: Defaults for the mailer. Currently the only option is from:

options - The Hash options used to refine the selection (default: {}):

:from       - Default from email address
:from_name  - Default from name
:merge_vars - Default merge vars

Examples

default from: 'foo@bar.com',
        from_name: 'Foo Bar',
        merge_vars: {'FOO' => 'Bar'}

Returns options

# File lib/mandrill_mailer/core_mailer.rb, line 146
def self.defaults
  @defaults || super_defaults || {}
end
super_defaults() click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 150
def self.super_defaults
  superclass.defaults if superclass.respond_to?(:defaults)
end
test(mailer_method, options={}) click to toggle source

Public: Executes a test email

mailer_method - Method to execute

options - The Hash options used to refine the selection (default: {}):

:email - The email to send the test to.

Examples

InvitationMailer.test(:invite, email: 'benny@envylabs.com')

Returns the duplicated String.

# File lib/mandrill_mailer/core_mailer.rb, line 202
def self.test(mailer_method, options={})
  unless options[:email]
    raise InvalidEmail.new 'Please specify a :email option(email to send the test to)'
  end

  if @mailer_methods[mailer_method]
    @mailer_methods[mailer_method].call(self.new, options)
  else
    raise InvalidMailerMethod.new "The mailer method: #{mailer_method} does not have test setup"
  end

end
test_setup_for(mailer_method, &block) click to toggle source

Public: setup a way to test mailer methods

mailer_method - Name of the mailer method the test setup is for

block - Block of code to execute to perform the test. The mailer and options are passed to the block. The options have to contain at least the :email to send the test to.

Examples

test_setup_for :invite do |mailer, options|
  invitation = OpenStruct.new({
    email: options[:email],
    owner_name: 'foobar',
    secret: rand(9000000..1000000).to_s
  })
  mailer.invite(invitation).deliver
end

Returns the duplicated String.

# File lib/mandrill_mailer/core_mailer.rb, line 185
def self.test_setup_for(mailer_method, &block)
  @mailer_methods ||= {}
  @mailer_methods[mailer_method] = block
end

Protected Class Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/mandrill_mailer/core_mailer.rb, line 297
def method_missing(method, *args, &block)
  return super unless respond_to?(method)
  new.method(method).call(*args, &block)
end
respond_to?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/mandrill_mailer/core_mailer.rb, line 305
def self.respond_to?(method, include_private = false)
  super || instance_methods.include?(method.to_sym)
end

Public Instance Methods

bcc() click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 275
def bcc
  self.message && self.message['bcc_address']
end
deliver() click to toggle source

Public: Triggers the stored Mandrill params to be sent to the Mandrill api

# File lib/mandrill_mailer/core_mailer.rb, line 216
def deliver
  raise NotImplementedError.new("#{self.class.name}#deliver is not implemented.")
end
deliver_later() click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 224
def deliver_later
  raise NotImplementedError.new("#{self.class.name}#deliver_later is not implemented.")
end
deliver_now() click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 220
def deliver_now
  raise NotImplementedError.new("#{self.class.name}#deliver_now is not implemented.")
end
from() click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 263
def from
  self.message && self.message['from_email']
end
mandrill_mail(args) click to toggle source

Public: Build the hash needed to send to the mandrill api

args - The Hash options used to refine the selection:

Examples

mandrill_mail template: 'Group Invite',
            subject: I18n.t('invitation_mailer.invite.subject'),
            to: invitation.email,
            vars: {
              'OWNER_NAME' => invitation.owner_name,
              'INVITATION_URL' => new_invitation_url(email: invitation.email, secret: invitation.secret)
            }

Returns the the mandrill mailer class (this is so you can chain deliver like a normal mailer)

# File lib/mandrill_mailer/core_mailer.rb, line 247
def mandrill_mail(args)
  extract_api_options!(args)

  # Call the mandrill_mail_handler so mailers can handle the args in custom ways
  mandrill_mail_handler(args)

  # Construct message hash
  self.message = MandrillMailer::ArgFormatter.format_messages_api_message_data(args, self.class.defaults)

  # Apply any interceptors that may be present
  apply_interceptors!(self.message)

  # return self so we can chain deliver after the method call, like a normal mailer.
  self
end
mandrill_mail_handler(args) click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 228
def mandrill_mail_handler(args)
  args
end
to() click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 267
def to
  self.message && self.message['to']
end
to=(values) click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 271
def to=(values)
  self.message && self.message['to'] = MandrillMailer::ArgFormatter.params(values)
end

Protected Instance Methods

api_key() click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 341
def api_key
  MandrillMailer.config.api_key
end
apply_interceptors!(obj) click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 281
def apply_interceptors!(obj)
  unless MandrillMailer.config.interceptor.nil?
    unless MandrillMailer.config.interceptor.is_a?(Proc)
      raise InvalidInterceptorParams.new "The interceptor_params config must be a proc"
    end
    MandrillMailer.config.interceptor.call(obj)
  end

  obj
end
extract_api_options!(args) click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 349
def extract_api_options!(args)
  self.async = args.delete(:async)
  self.ip_pool = args.delete(:ip_pool)

  if args.has_key?(:send_at)
    self.send_at = args.delete(:send_at).getutc.strftime('%Y-%m-%d %H:%M:%S')
  end
end
image_path(image) click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 329
def image_path(image)
  if defined? Rails
    ActionController::Base.helpers.asset_path(image)
  else
    method_missing(:image_path, image)
  end
end
image_url(image) click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 337
def image_url(image)
  "#{root_url}#{image_path(image).split('/').reject!(&:empty?).join('/')}"
end
mandrill_api() click to toggle source
# File lib/mandrill_mailer/core_mailer.rb, line 345
def mandrill_api
  @mandrill_api ||= Mandrill::API.new(api_key)
end
method_missing(method, *args) click to toggle source

Proxy route helpers to rails if Rails exists. Doing routes this way makes it so this gem doesn't need to be a rails engine

Calls superclass method
# File lib/mandrill_mailer/core_mailer.rb, line 311
def method_missing(method, *args)
  return super unless defined?(Rails) && Rails.application.routes.url_helpers.respond_to?(method)
  # Check to see if one of the args is an open struct. If it is, we'll assume it's the
  # test stub and try to call a path or url attribute.
  if args.any? {|arg| arg.kind_of?(MandrillMailer::Mock)}
    # take the first OpenStruct found in args and look for .url or.path
    args.each do |arg|
      if arg.kind_of?(MandrillMailer::Mock)
        break arg.url || arg.path
      end
    end
  else
    options = args.extract_options!.merge({host: MandrillMailer.config.default_url_options[:host], protocol: MandrillMailer.config.default_url_options[:protocol]})
    args << options
    Rails.application.routes.url_helpers.method(method).call(*args)
  end
end