Class: ActionMailbox::Base
- Includes:
- Callbacks, Routing, ActiveSupport::Rescuable
- Defined in:
- actionmailbox/lib/action_mailbox/base.rb
Overview
Action Mailbox Base
The base class for all application mailboxes. Not intended to be inherited from directly. Inherit from ApplicationMailbox
instead, as that’s where the app-specific routing is configured. This routing is specified in the following ways:
class ApplicationMailbox < ActionMailbox::Base
# Any of the recipients of the mail (whether to, cc, bcc) are matched against the regexp.
routing /^replies@/i => :replies
# Any of the recipients of the mail (whether to, cc, bcc) needs to be an exact match for the string.
routing "[email protected]" => :help
# Any callable (proc, lambda, etc) object is passed the inbound_email record and is a match if true.
routing ->(inbound_email) { inbound_email.mail.to.size > 2 } => :multiple_recipients
# Any object responding to #match? is called with the inbound_email record as an argument. Match if true.
routing CustomAddress.new => :custom
# Any inbound_email that has not been already matched will be sent to the BackstopMailbox.
routing :all => :backstop
end
Application mailboxes need to override the #process method, which is invoked by the framework after callbacks have been run. The callbacks available are: before_processing
, after_processing
, and around_processing
. The primary use case is to ensure that certain preconditions to processing are fulfilled using before_processing
callbacks.
If a precondition fails to be met, you can halt the processing using the #bounced!
method, which will silently prevent any further processing, but not actually send out any bounce notice. You can also pair this behavior with the invocation of an Action Mailer class responsible for sending out an actual bounce email. This is done using the #bounce_with method, which takes the mail object returned by an Action Mailer method, like so:
class ForwardsMailbox < ApplicationMailbox
before_processing :ensure_sender_is_a_user
private
def ensure_sender_is_a_user
unless User.exist?(email_address: mail.from)
bounce_with UserRequiredMailer.missing(inbound_email)
end
end
end
During the processing of the inbound email, the status will be tracked. Before processing begins, the email will normally have the pending
status. Once processing begins, just before callbacks and the #process method is called, the status is changed to processing
. If processing is allowed to complete, the status is changed to delivered
. If a bounce is triggered, then bounced
. If an unhandled exception is bubbled up, then failed
.
Exceptions can be handled at the class level using the familiar Rescuable
approach:
class ForwardsMailbox < ApplicationMailbox
rescue_from(ApplicationSpecificVerificationError) { bounced! }
end
Constant Summary
Constants included from Callbacks
Constants included from ActiveSupport::Callbacks
ActiveSupport::Callbacks::CALLBACK_FILTER_TYPES
Instance Attribute Summary collapse
-
#inbound_email ⇒ Object
readonly
Returns the value of attribute inbound_email.
Class Method Summary collapse
Instance Method Summary collapse
-
#bounce_with(message) ⇒ Object
Enqueues the given
message
for delivery and changes the inbound email’s status to:bounced
. -
#finished_processing? ⇒ Boolean
:nodoc:.
-
#initialize(inbound_email) ⇒ Base
constructor
A new instance of Base.
-
#perform_processing ⇒ Object
:nodoc:.
- #process ⇒ Object
Methods included from ActiveSupport::Concern
#append_features, #class_methods, extended, #included, #prepend_features, #prepended
Methods included from ActiveSupport::Callbacks
Methods included from ActiveSupport::Rescuable
#handler_for_rescue, #rescue_with_handler
Constructor Details
#initialize(inbound_email) ⇒ Base
Returns a new instance of Base.
78 79 80 |
# File 'actionmailbox/lib/action_mailbox/base.rb', line 78 def initialize(inbound_email) @inbound_email = inbound_email end |
Instance Attribute Details
#inbound_email ⇒ Object (readonly)
Returns the value of attribute inbound_email
69 70 71 |
# File 'actionmailbox/lib/action_mailbox/base.rb', line 69 def inbound_email @inbound_email end |
Class Method Details
.receive(inbound_email) ⇒ Object
74 75 76 |
# File 'actionmailbox/lib/action_mailbox/base.rb', line 74 def self.receive(inbound_email) new(inbound_email).perform_processing end |
Instance Method Details
#bounce_with(message) ⇒ Object
Enqueues the given message
for delivery and changes the inbound email’s status to :bounced
.
105 106 107 108 |
# File 'actionmailbox/lib/action_mailbox/base.rb', line 105 def bounce_with() inbound_email.bounced! .deliver_later end |
#finished_processing? ⇒ Boolean
:nodoc:
99 100 101 |
# File 'actionmailbox/lib/action_mailbox/base.rb', line 99 def finished_processing? # :nodoc: inbound_email.delivered? || inbound_email.bounced? end |
#perform_processing ⇒ Object
:nodoc:
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'actionmailbox/lib/action_mailbox/base.rb', line 82 def perform_processing # :nodoc: ActiveSupport::Notifications.instrument "process.action_mailbox", instrumentation_payload do track_status_of_inbound_email do run_callbacks :process do process end end rescue => exception # TODO: Include a reference to the inbound_email in the exception raised so error handling becomes easier rescue_with_handler(exception) || raise end end |
#process ⇒ Object
95 96 97 |
# File 'actionmailbox/lib/action_mailbox/base.rb', line 95 def process # Override in subclasses end |