Class: MandrillMailer::TemplateMailer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
lib/mandrill_mailer/offline.rb,
lib/mandrill_mailer/template_mailer.rb

Defined Under Namespace

Classes: InvalidEmail, InvalidInterceptorParams, InvalidMailerMethod

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)

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



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/mandrill_mailer/template_mailer.rb', line 373

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

Class Attribute Details

.defaultsObject

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

Examples

default from: '[email protected]'

Returns options



123
124
125
# File 'lib/mandrill_mailer/template_mailer.rb', line 123

def self.defaults
  @defaults || super_defaults
end

Instance Attribute Details

#asyncObject

Public: Enable background sending mode



200
201
202
# File 'lib/mandrill_mailer/template_mailer.rb', line 200

def async
  @async
end

#ip_poolObject

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



203
204
205
# File 'lib/mandrill_mailer/template_mailer.rb', line 203

def ip_pool
  @ip_pool
end

#messageObject

Public: Other information on the message to send



197
198
199
# File 'lib/mandrill_mailer/template_mailer.rb', line 197

def message
  @message
end

#send_atObject

Public: When message should be sent



206
207
208
# File 'lib/mandrill_mailer/template_mailer.rb', line 206

def send_at
  @send_at
end

#template_contentObject

Public: Template content



194
195
196
# File 'lib/mandrill_mailer/template_mailer.rb', line 194

def template_content
  @template_content
end

#template_nameObject

Public: The name of the template to use



191
192
193
# File 'lib/mandrill_mailer/template_mailer.rb', line 191

def template_name
  @template_name
end

Class Method Details

.default(args) ⇒ Object



131
132
133
134
135
# File 'lib/mandrill_mailer/template_mailer.rb', line 131

def self.default(args)
  @defaults ||= {}
  @defaults[:from] ||= '[email protected]'
  @defaults.merge!(args)
end

.super_defaultsObject



127
128
129
# File 'lib/mandrill_mailer/template_mailer.rb', line 127

def self.super_defaults
  superclass.defaults if superclass.respond_to?(:defaults)
end

.test(mailer_method, options = {}) ⇒ Object

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: '[email protected]')

Returns the duplicated String.



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/mandrill_mailer/template_mailer.rb', line 177

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) ⇒ Object

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.



160
161
162
163
# File 'lib/mandrill_mailer/template_mailer.rb', line 160

def self.test_setup_for(mailer_method, &block)
  @mailer_methods ||= {}
  @mailer_methods[mailer_method] = block
end

Instance Method Details

#bccObject



331
332
333
# File 'lib/mandrill_mailer/template_mailer.rb', line 331

def bcc
  self.message && self.message['bcc_address']
end

#dataObject

Public: Data hash (deprecated)



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/mandrill_mailer/template_mailer.rb', line 311

def data
  {
    "key" => api_key,
    "template_name" => template_name,
    "template_content" => template_content,
    "message" => message,
    "async" => async,
    "ip_pool" => ip_pool,
    "send_at" => send_at
  }
end

#deliverObject

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

text - The String to be duplicated. count - The Integer number of times to duplicate the text.

Examples

multiplex('Tom', 4)
# => 'TomTomTomTom'

Returns the duplicated String.



219
220
221
222
223
224
225
226
227
228
# File 'lib/mandrill_mailer/template_mailer.rb', line 219

def deliver
  MandrillMailer.deliveries << MandrillMailer::Mock.new({
    :template_name    => template_name,
    :template_content => template_content,
    :message          => message,
    :async            => async,
    :ip_pool          => ip_pool,
    :send_at          => send_at
  })
end

#fromObject



323
324
325
# File 'lib/mandrill_mailer/template_mailer.rb', line 323

def from
  self.message && self.message['from_email']
end

#mandrill_mail(args) ⇒ Object

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

args - The Hash options used to refine the selection:

:template - Template name in Mandrill
:subject - Subject of the email
:to - Email to send the mandrill email to
:vars - Global merge vars used in the email for dynamic data
:recipient_vars - Merge vars used in the email for recipient-specific dynamic data
:bcc - bcc email for the mandrill email
:tags - Tags for the email
:google_analytics_domains - Google analytics domains
:google_analytics_campaign - Google analytics campaign
:inline_css - whether or not to automatically inline all CSS styles provided in the message HTML
:important - whether or not this message is important
:async - whether or not this message should be sent asynchronously
:ip_pool - name of the dedicated IP pool that should be used to send the message
:send_at - when this message should be sent

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)



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/mandrill_mailer/template_mailer.rb', line 253

def mandrill_mail(args)

  # Mandrill requires template content to be there
  args[:template_content] = {"blank" => ""} if args[:template_content].blank?

  # format the :to param to what Mandrill expects if a string or array is passed
  args[:to] = format_to_params(args[:to])

  # Set the template name
  self.template_name = args.delete(:template)

  # Set the template content
  self.template_content = mandrill_args(args.delete(:template_content))

  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

  # Construct message hash
  self.message = {
    "subject" => args[:subject],
    "from_email" => args[:from] || self.class.defaults[:from],
    "from_name" => args[:from_name] || self.class.defaults[:from_name] || self.class.defaults[:from],
    "to" => args[:to],
    "headers" => args[:headers],
    "important" => args[:important],
    "track_opens" => args.fetch(:track_opens, true),
    "track_clicks" => args.fetch(:track_clicks, true),
    "auto_text" => true,
    "inline_css" => args[:inline_css],
    "url_strip_qs" => args.fetch(:url_strip_qs, true),
    "preserve_recipients" => args[:preserve_recipients],
    "bcc_address" => args[:bcc],
    "global_merge_vars" => mandrill_args(args[:vars]),
    "merge_vars" => mandrill_rcpt_args(args[:recipient_vars]),
    "tags" => args[:tags],
    "subaccount" => args[:subaccount],
    "google_analytics_domains" => args[:google_analytics_domains],
    "google_analytics_campaign" => args[:google_analytics_campaign],
    "metadata" => args[:metadata],
    "attachments" => mandrill_attachment_args(args[:attachments]),
    "images" => mandrill_images_args(args[:images])
  }

  unless MandrillMailer.config.interceptor_params.nil?
    unless MandrillMailer.config.interceptor_params.is_a?(Hash)
      raise InvalidInterceptorParams.new "The interceptor_params config must be a Hash"
    end
    self.message.merge!(MandrillMailer.config.interceptor_params.stringify_keys)
  end

  # return self so we can chain deliver after the method call, like a normal mailer.
  return self
end

#toObject



327
328
329
# File 'lib/mandrill_mailer/template_mailer.rb', line 327

def to
  self.message && self.message['to']
end