class PostageApp::Mailer

Postage::Mailer allows you to use/re-use existing mailers set up using ActionMailer. The only catch is to change inheritance from ActionMailer::Base to PostageApp::Mailer. Also don't forget to require 'postageapp/mailer'

Here's an example of a valid PostageApp::Mailer class

require 'postageapp/mailer'

class Notifier < PostageApp::Mailer
  def signup_notification(recipient)
    mail(
      to: recipient.email,
      from: 'sender@test.test',
      subject: 'Test Message'
    )
  end
end

Postage::Mailer introduces a few mailer methods specific to Postage:

Sending email

# Create a PostageApp::Request object
request = Notifier.signup_notification(user)
# Deliver the message and return a PostageApp::Response
response = request.deliver_now

Constants

CONTENT_TYPE_MAP

Public Class Methods

new(method_name = nil, *args) click to toggle source

Instead of initializing Mail object, we prepare PostageApp::Request

Calls superclass method
# File lib/postageapp/mailer/mailer_4.rb, line 70
def initialize(method_name = nil, *args)
  super()

  @_message = PostageApp::Request.new(:send_message)

  if (method_name)
    process(method_name, *args)
  end
end

Public Instance Methods

attachments() click to toggle source
# File lib/postageapp/mailer/mailer_4.rb, line 116
def attachments
  @_attachments ||= Attachments.new(@_message)
end
cc() click to toggle source
# File lib/postageapp/mailer/mailer_4.rb, line 183
def cc
  @_message.arguments.dig('headers', 'cc')
end
find_first_mime_type(mt) click to toggle source
# File lib/postageapp/mailer/mailer_4.rb, line 169
def find_first_mime_type(mt)
  part = arguments['content'].detect{ |mime_type, body| mime_type == mt }

  OpenStruct.new(mime_type: part[0], decoded: part[1]) if part
end
header() click to toggle source
# File lib/postageapp/mailer/mailer_4.rb, line 175
def header
  @_message.arguments['headers']
end
headers(args = nil) click to toggle source

Override for headers assignment

# File lib/postageapp/mailer/mailer_4.rb, line 121
def headers(args = nil)
  @_message.headers(args)
end
mail(headers = { }, &block) click to toggle source

Overriding method that prepares Mail object. This time we'll be contructing PostageApp::Request payload.

# File lib/postageapp/mailer/mailer_4.rb, line 127
def mail(headers = { }, &block)
  # Guard flag to prevent both the old and the new API from firing
  # Should be removed when old API is removed
  @_mail_was_called = true
  m = @_message

  # Call all the procs (if any)
  class_default = self.class.default
  default_values = class_default.merge(self.class.default) do |k,v|
    v.respond_to?(:call) ? v.bind(self).call : v
  end

  # Handle defaults
  headers = headers.reverse_merge(default_values)

  # Set configure delivery behavior
  wrap_delivery_behavior!(
    headers.delete(:delivery_method),
    headers.delete(:delivery_method_options)
  )

  # Assigning recipients
  m.arguments['recipients'] = headers.delete(:to)

  # Assign all headers except parts_order, content_type and body
  assignable = headers.except(
    :parts_order,
    :content_type,
    :body,
    :template_name,
    :template_path
  )

  m.headers.merge!(assignable)

  # Render the templates and blocks
  responses = collect_responses(headers, &block)
  create_parts_from_responses(m, responses)

  m
end
multipart?() click to toggle source
# File lib/postageapp/mailer/mailer_4.rb, line 187
def multipart?
  %w[ text/plain text/html ].all? { |mt| arguments['content'].key?(mt) }
end
postageapp_api_key(value = nil) click to toggle source
# File lib/postageapp/mailer/mailer_4.rb, line 89
def postageapp_api_key(value = nil)
  if (value)
    @_message.api_key = value
  else
    @_message.api_key
  end
end
postageapp_template(value = nil) click to toggle source

In API call we can specify PostageApp template that will be used to generate content of the message

# File lib/postageapp/mailer/mailer_4.rb, line 99
def postageapp_template(value = nil)
  if (value)
    @_message.arguments['template'] = value
  else
    @_message.arguments['template']
  end
end
postageapp_uid(value = nil) click to toggle source

Possible to define custom uid. Should be sufficiently unique

# File lib/postageapp/mailer/mailer_4.rb, line 81
def postageapp_uid(value = nil)
  if (value)
    @_message.uid = value
  else
    @_message.uid
  end
end
postageapp_variables(value = nil) click to toggle source

Hash of variables that will be used to inject into the content

# File lib/postageapp/mailer/mailer_4.rb, line 108
def postageapp_variables(value = nil)
  if (value)
    @_message.arguments['variables'] = value
  else
    @_message.arguments['variables']
  end
end
reply_to() click to toggle source
# File lib/postageapp/mailer/mailer_4.rb, line 179
def reply_to
  @_message.arguments.dig('headers', 'reply_to')
end