Class: Mailpace::DeliveryMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/mailpace-rails.rb

Overview

MailPace ActionMailer delivery method

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ DeliveryMethod

Returns a new instance of DeliveryMethod.



14
15
16
17
# File 'lib/mailpace-rails.rb', line 14

def initialize(values)
  check_api_token(values)
  self.settings = { return_response: true }.merge!(values)
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



12
13
14
# File 'lib/mailpace-rails.rb', line 12

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mailpace-rails.rb', line 19

def deliver!(mail)
  check_delivery_params(mail)
  result = HTTParty.post(
    'https://app.mailpace.com/api/v1/send',
    body: {
      from: address_list(mail.header[:from])&.addresses&.first.to_s,
      to: mail.to.join(','),
      subject: mail.subject,
      htmlbody: mail.html_part ? mail.html_part.body.decoded : mail.body.to_s,
      textbody: if mail.multipart?
                  mail.text_part ? mail.text_part.body.decoded : nil
                end,
      cc: address_list(mail.header[:cc])&.addresses&.join(','),
      bcc: address_list(mail.header[:bcc])&.addresses&.join(','),
      replyto: mail.reply_to&.join(','),
      inreplyto: mail.header['In-Reply-To'].to_s,
      references: mail.header['References'].to_s,
      list_unsubscribe: mail.header['list_unsubscribe'].to_s,
      attachments: format_attachments(mail.attachments),
      tags: mail.header['tags'].to_s
    }.delete_if { |_key, value| value.blank? }.to_json,
    headers: {
      'User-Agent' => "MailPace Rails Gem v#{Mailpace::Rails::VERSION}",
      'Accept' => 'application/json',
      'Content-Type' => 'application/json',
      'Mailpace-Server-Token' => settings[:api_token]
    }.tap do |h|
      h['Idempotency-Key'] = mail.header['idempotency_key'].to_s if mail.header['idempotency_key']
    end
  )

  handle_response(result)
end