Class: PostageApp::Mailer

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/postageapp/mailer/mailer_2.rb,
lib/postageapp/mailer/mailer_3.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

request = Notifier.(user) # creates PostageApp::Request object
response = request.deliver # attempts to deliver the message and creates a PostageApp::Response

Defined Under Namespace

Classes: Attachments

Instance Method Summary collapse

Constructor Details

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

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



61
62
63
64
65
# File 'lib/postageapp/mailer/mailer_3.rb', line 61

def initialize(method_name = nil, *args)
  super()
  @_message = PostageApp::Request.new(:send_message)
  process(method_name, *args) if method_name
end

Instance Method Details

#attachmentsObject



87
88
89
# File 'lib/postageapp/mailer/mailer_3.rb', line 87

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

#create_mailObject

Creating a Postage::Request object unlike TMail one in ActionMailer::Base



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/postageapp/mailer/mailer_2.rb', line 47

def create_mail
  params = { }
  params['recipients'] = self.recipients unless self.recipients.blank?
  
  params['headers'] = { }
  params['headers']['subject']  = self.subject  unless self.subject.blank?
  params['headers']['from']     = self.from     unless self.from.blank?
  params['headers'].merge!(self.headers)        unless self.headers.blank?
  
  params['content'] = { }
  params['attachments'] = { }
  
  if @parts.empty?
    params['content'][self.content_type] = self.body unless self.body.blank?
  else
    self.parts.each do |part|
      case part.content_disposition
      when 'inline'
        part.content_type = 'text/plain' if part.content_type.blank? && String === part.body
        params['content'][part.content_type] = part.body
      when 'attachment'
        params['attachments'][part.filename] = {
          'content_type' => part.content_type,
          'content'      => Base64.encode64(part.body)
        }
      end
    end
  end
  
  params['template']  = self.postageapp_template  unless self.postageapp_template.blank?
  params['variables'] = self.postageapp_variables unless self.postageapp_variables.blank?
  
  params.delete('headers')     if params['headers'].blank?
  params.delete('content')     if params['content'].blank?
  params.delete('attachments') if params['attachments'].blank?
  
  @mail = PostageApp::Request.new('send_message', params)
  @mail.uid     = self.postageapp_uid     unless self.postageapp_uid.blank?
  @mail.api_key = self.postageapp_api_key unless self.postageapp_api_key.blank?
  @mail
end

#deliver!(mail = @mail) ⇒ Object



41
42
43
44
# File 'lib/postageapp/mailer/mailer_2.rb', line 41

def deliver!(mail = @mail)
  raise 'PostageApp::Request object not present, cannot deliver' unless mail
  __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
end

#headers(args = nil) ⇒ Object

Override for headers assignment



92
93
94
# File 'lib/postageapp/mailer/mailer_3.rb', line 92

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.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/postageapp/mailer/mailer_3.rb', line 98

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
  
  # At the beginning, do not consider class default for parts order neither content_type
  content_type = headers[:content_type]
  parts_order  = headers[:parts_order]
  
  # Call all the procs (if any)
  default_values = self.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)
  headers[:subject] ||= default_i18n_subject
  
  # Set configure delivery behavior
  wrap_delivery_behavior!(headers.delete(:delivery_method))
  
  # 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, explicit_order = collect_responses_and_parts_order(headers, &block)
  create_parts_from_responses(m, responses)
  
  m
end

#perform_delivery_postage(mail) ⇒ Object



37
38
39
# File 'lib/postageapp/mailer/mailer_2.rb', line 37

def perform_delivery_postage(mail)
  mail.send
end

#postageapp_api_key(value = nil) ⇒ Object



72
73
74
# File 'lib/postageapp/mailer/mailer_3.rb', line 72

def postageapp_api_key(value = nil)
  value ? @_message.api_key = value : @_message.api_key
end

#postageapp_template(value = nil) ⇒ Object

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



78
79
80
# File 'lib/postageapp/mailer/mailer_3.rb', line 78

def postageapp_template(value = nil)
  value ? @_message.arguments['template'] = value : @_message.arguments['template']
end

#postageapp_uid(value = nil) ⇒ Object

Possible to define custom uid. Should be sufficiently unique



68
69
70
# File 'lib/postageapp/mailer/mailer_3.rb', line 68

def postageapp_uid(value = nil)
  value ? @_message.uid = value : @_message.uid
end

#postageapp_variables(value = nil) ⇒ Object

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



83
84
85
# File 'lib/postageapp/mailer/mailer_3.rb', line 83

def postageapp_variables(value = nil)
  value ? @_message.arguments['variables'] = value : @_message.arguments['variables']
end

#render(opts) ⇒ Object

Not insisting rendering a view if it’s not there. PostageApp gem can send blank content provided that the template is defined.



91
92
93
94
95
# File 'lib/postageapp/mailer/mailer_2.rb', line 91

def render(opts)
  super(opts)
rescue ActionView::MissingTemplate
  # do nothing
end