aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/action_mailroom/inbound_email.rb
blob: 790e0ccc4f24f5221d84ca9a77c5ad34cf70e3db (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
require "mail"

# TODO: Add email_message_id to the record extracted from raw_email.message_id to make tracing emails easier
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_raw_content(raw_email_content)
      Mail.new(Mail::Utilities.binary_unsafe_to_crlf(raw_email_content.to_s))
    end

    private
      def extract_message_id(raw_email)
        mail_from_raw_content(raw_email.read).message_id
      end
  end

  def mail
    @mail ||= self.class.mail_from_raw_content(raw_email.download)
  end
end