Class: Ohmysmtp::DeliveryMethod

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

Overview

OhMySMTP 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/ohmysmtp-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/ohmysmtp-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
# File 'lib/ohmysmtp-rails.rb', line 19

def deliver!(mail)
  check_delivery_params(mail)
  result = HTTParty.post(
    'https://app.ohmysmtp.com/api/v1/send',
    body: {
      from: mail.header[:from]&.address_list&.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: mail.cc&.join(','),
      bcc: mail.bcc&.join(','),
      replyto: mail.reply_to,
      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' => "OhMySMTP Rails Gem v#{Ohmysmtp::Rails::VERSION}",
      'Accept' => 'application/json',
      'Content-Type' => 'application/json',
      'Ohmysmtp-Server-Token' => settings[:api_token]
    }
  )

  handle_response(result)
end