2018-12-14 05:06:12 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-18 01:15:27 -04:00
|
|
|
require "mail"
|
|
|
|
|
2018-12-12 19:34:05 -05:00
|
|
|
# The `InboundEmail` is an Active Record that keeps a reference to the raw email stored in Active Storage
|
|
|
|
# and tracks the status of processing. By default, incoming emails will go through the following lifecycle:
|
|
|
|
#
|
|
|
|
# * Pending: Just received by one of the ingress controllers and scheduled for routing.
|
|
|
|
# * Processing: During active processing, while a specific mailbox is running its #process method.
|
|
|
|
# * Delivered: Successfully processed by the specific mailbox.
|
|
|
|
# * Failed: An exception was raised during the specific mailbox's execution of the `#process` method.
|
|
|
|
# * Bounced: Rejected processing by the specific mailbox and bounced to sender.
|
|
|
|
#
|
|
|
|
# Once the `InboundEmail` has reached the status of being either `delivered`, `failed`, or `bounced`,
|
|
|
|
# it'll count as having been `#processed?`. Once processed, the `InboundEmail` will be scheduled for
|
|
|
|
# automatic incineration at a later point.
|
|
|
|
#
|
|
|
|
# When working with an `InboundEmail`, you'll usually interact with the parsed version of the source,
|
|
|
|
# which is available as a `Mail` object from `#mail`. But you can also access the raw source directly
|
|
|
|
# using the `#source` method.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# inbound_email.mail.from # => 'david@loudthinking.com'
|
|
|
|
# inbound_email.source # Returns the full rfc822 source of the email as text
|
2018-09-28 15:19:43 -04:00
|
|
|
class ActionMailbox::InboundEmail < ActiveRecord::Base
|
|
|
|
self.table_name = "action_mailbox_inbound_emails"
|
2018-09-17 19:55:07 -04:00
|
|
|
|
2018-09-28 14:01:49 -04:00
|
|
|
include Incineratable, MessageId, Routable
|
2018-09-17 19:55:07 -04:00
|
|
|
|
2018-09-19 19:40:56 -04:00
|
|
|
has_one_attached :raw_email
|
2018-09-17 19:55:07 -04:00
|
|
|
enum status: %i[ pending processing delivered failed bounced ]
|
|
|
|
|
|
|
|
def mail
|
2018-11-05 19:36:21 -05:00
|
|
|
@mail ||= Mail.from_source(source)
|
2018-09-27 20:37:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def source
|
|
|
|
@source ||= raw_email.download
|
2018-09-17 19:55:07 -04:00
|
|
|
end
|
2018-10-17 10:38:51 -04:00
|
|
|
|
|
|
|
def processed?
|
|
|
|
delivered? || failed? || bounced?
|
|
|
|
end
|
2018-09-17 19:55:07 -04:00
|
|
|
end
|