Class: DraftBox::DeliveryMethod

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DeliveryMethod

Returns a new instance of DeliveryMethod.



5
6
7
# File 'lib/draft_box/delivery_method.rb', line 5

def initialize(options = {})
  self.settings = options
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



3
4
5
# File 'lib/draft_box/delivery_method.rb', line 3

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/draft_box/delivery_method.rb', line 9

def deliver!(mail)
  validate_mail!(mail)

  ActiveRecord::Base.transaction do
    email = DraftBox::Email.create!(
      subject: mail.subject,
      body: mail.parts.empty? ? mail.body.to_s : mail.parts.find { |p| p.content_type.start_with?('text/html', 'text/plain') }&.body.to_s,
      from: mail.from.first,
      recipients: mail.to,
      carbon_copies: mail.cc || [],
      blind_copies: mail.bcc || []
    )

    mail.attachments.each do |attachment|
      email.attachments.create!(
        filename: attachment.filename,
        content_type: attachment.content_type,
        data: attachment.body.to_s
      )
    end
  end
end