Class: PostageApp::Mailer

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/postageapp/mailer/mailer_4.rb

Overview

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 (recipient)
    mail(
      to: recipient.email,
      from: '[email protected]',
      subject: 'Test Message'
    )
  end
end

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

  • postageapp_template - template name that is defined in your PostageApp project

  • postageapp_variables - extra variables you want to send along with the message

Sending email

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

Defined Under Namespace

Classes: Attachments

Constant Summary collapse

CONTENT_TYPE_MAP =
{
  'html' => 'text/html',
  'text' => 'text/plain'
}

Instance Method Summary collapse

Constructor Details

#initialize(method_name = nil, *args) ⇒ Mailer

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



70
71
72
73
74
75
76
77
78
# 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

Instance Method Details

#attachmentsObject



116
117
118
# File 'lib/postageapp/mailer/mailer_4.rb', line 116

def attachments
  @_attachments ||= Attachments.new(@_message)
end

#ccObject



183
184
185
# File 'lib/postageapp/mailer/mailer_4.rb', line 183

def cc
  @_message.arguments.dig('headers', 'cc')
end

#find_first_mime_type(mt) ⇒ Object



169
170
171
172
173
# 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

#headerObject



175
176
177
# File 'lib/postageapp/mailer/mailer_4.rb', line 175

def header
  @_message.arguments['headers']
end

#headers(args = nil) ⇒ Object

Override for headers assignment



121
122
123
# File 'lib/postageapp/mailer/mailer_4.rb', line 121

def headers(args = nil)
  @_message.headers(args)
end

#mail(headers = { }, &block) ⇒ Object

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 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?Boolean

Returns:

  • (Boolean)


187
188
189
# 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) ⇒ Object



89
90
91
92
93
94
95
# 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) ⇒ Object

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



99
100
101
102
103
104
105
# 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) ⇒ Object

Possible to define custom uid. Should be sufficiently unique



81
82
83
84
85
86
87
# 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) ⇒ Object

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



108
109
110
111
112
113
114
# 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_toObject



179
180
181
# File 'lib/postageapp/mailer/mailer_4.rb', line 179

def reply_to
  @_message.arguments.dig('headers', 'reply_to')
end