Class: MailchannelsWorker::DeliveryMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/mailchannels_worker.rb

Constant Summary collapse

RESERVED_HEADERS =
%w[received dkim-signature Content-Type Content-Transfer-Encoding Message-ID To From Subject Reply-To CC BCC].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ DeliveryMethod

Returns a new instance of DeliveryMethod.



18
19
20
21
# File 'lib/mailchannels_worker.rb', line 18

def initialize(settings = {})
  @url = settings[:url]
  @api_key = settings[:api_key]
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



16
17
18
# File 'lib/mailchannels_worker.rb', line 16

def api_key
  @api_key
end

#urlObject (readonly)

Returns the value of attribute url.



16
17
18
# File 'lib/mailchannels_worker.rb', line 16

def url
  @url
end

Instance Method Details

#deliver!(mail) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mailchannels_worker.rb', line 23

def deliver!(mail)
  body = {
    from: { email: mail.from_address.address, name: mail.from_address.name }.compact,
    subject: mail.subject,
    personalizations: [{
      headers: mail.header.fields.map { |field| [field.name, field.value] unless field.name.in?(RESERVED_HEADERS) }.compact.to_h.presence,
      to: mail.to_addresses.map { |address| { email: address.address, name: address.name }.compact },
      cc: mail.cc_addresses.map { |address| { email: address.address, name: address.name }.compact }.presence,
      bcc: mail.bcc_addresses.map { |address| { email: address.address, name: address.name }.compact }.presence,
      reply_to: (mail.header["Reply-To"]&.element&.addresses || []).map { |address| { email: address.address, name: address.name }.compact }.first
    }.compact],
    content: mail.multipart? ? mail.parts.map { |part| { type: part.content_type, value: part.decoded } } : [{ type: mail.content_type, value: mail.decoded }]
  }
  send_email(body)
end