aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/action_mailbox/inbound_email.rb
blob: 7d1a36b705d72de7020c7d9b10af697fac1293ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require "mail"

class ActionMailbox::InboundEmail < ActiveRecord::Base
  self.table_name = "action_mailbox_inbound_emails"

  include Incineratable, MessageId, Routable

  has_one_attached :raw_email
  enum status: %i[ pending processing delivered failed bounced ]

  def self.mail_from_source(source)
    Mail.new Mail::Utilities.binary_unsafe_to_crlf(source.to_s)
  end

  def mail
    @mail ||= self.class.mail_from_source(source)
  end

  def source
    @source ||= raw_email.download
  end

  def processed?
    delivered? || failed? || bounced?
  end
end