aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/action_mailroom/inbound_email.rb
blob: 6dd643073a67947863777296f85b1c4f4bb829b2 (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
27
28
29
30
31
32
33
34
35
require "mail"

class ActionMailroom::InboundEmail < ActiveRecord::Base
  self.table_name = "action_mailroom_inbound_emails"

  include Incineratable, Routable

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

  class << self
    def create_from_raw_email!(raw_email, **options)
      create! raw_email: raw_email, message_id: extract_message_id(raw_email), **options
    end
    
    def mail_from_source(source)
      Mail.new(Mail::Utilities.binary_unsafe_to_crlf(source.to_s))
    end

    private
      def extract_message_id(raw_email)
        mail_from_source(raw_email.read).message_id
      rescue => e
        # TODO: Assign message id if it can't be extracted?
      end
  end

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

  def source
    @source ||= raw_email.download
  end
end