File: inbound_email.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (55 lines) | stat: -rw-r--r-- 2,042 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

require "mail"

module ActionMailbox
  # 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
  class InboundEmail < Record
    include Incineratable, MessageId, Routable

    has_one_attached :raw_email, service: ActionMailbox.storage_service
    enum :status, %i[ pending processing delivered failed bounced ]

    def mail
      @mail ||= Mail.from_source(source)
    end

    def source
      @source ||= raw_email.download
    end

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

    def instrumentation_payload # :nodoc:
      {
        id: id,
        message_id: message_id,
        status: status
      }
    end
  end
end

ActiveSupport.run_load_hooks :action_mailbox_inbound_email, ActionMailbox::InboundEmail