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
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/draft_box/delivery_method.rb', line 9

def deliver!(mail)
  validate_mail!(mail)

  # subject: mail.subject
  # body: mail.body.to_s
  # from: mail.from
  # to: mail.to
  # cc: mail.cc
  # bcc: mail.bcc
  # attachments: mail.attachments

  # Attachment:
  # filename: attachment.filename
  # content_type: attachment.content_type
  # content: attachment.body.to_s

  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,
      to: mail.to,
      cc: mail.cc,
      bcc: 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