Action Mailbox 基礎類別
所有應用程式信箱的基底類別。不應直接繼承。請繼承 ApplicationMailbox
,因為應用程式特定的路由設定在此處。路由設定方式如下
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 "help@example.com" => :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
應用程式信箱需要覆寫 process
方法,框架會在執行回呼後呼叫此方法。可用的回呼有:before_processing
、after_processing
和 around_processing
。主要用例是使用 before_processing
回呼確保滿足處理的某些前提條件。
如果未滿足前提條件,您可以使用 #bounced!
方法停止處理,這將靜默地阻止任何進一步的處理,但實際上不會發送任何退信通知。您也可以將此行為與負責發送實際退信郵件的 Action Mailer 類別的呼叫配對。這是使用 bounce_with
方法完成的,該方法接受 Action Mailer 方法返回的郵件物件,如下所示
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
在處理收件郵件期間,將會追蹤狀態。在開始處理之前,郵件通常會處於 pending
(待處理)狀態。一旦開始處理,就在回呼和 process
方法被呼叫之前,狀態會更改為 processing
(處理中)。如果允許處理完成,狀態將更改為 delivered
(已遞送)。如果觸發退信,則為 bounced
(已退信)。如果未處理的例外被拋出,則為 failed
(失敗)。
可以使用熟悉的 ActiveSupport::Rescuable
方法在類別層級處理例外
class ForwardsMailbox < ApplicationMailbox
rescue_from(ApplicationSpecificVerificationError) { bounced! }
end
- B
- N
- P
- R
屬性
[R] | inbound_email |
類別公開方法
new(inbound_email) 連結
來源:顯示 | 在 GitHub 上
# File actionmailbox/lib/action_mailbox/base.rb, line 79 def initialize(inbound_email) @inbound_email = inbound_email end
receive(inbound_email) 連結
來源:顯示 | 在 GitHub 上
# File actionmailbox/lib/action_mailbox/base.rb, line 75 def self.receive(inbound_email) new(inbound_email).perform_processing end
實例公開方法
bounce_now_with(message) 連結
立即發送指定的 message
並將收件郵件的狀態更改為 :bounced
(已退信)。
來源:顯示 | 在 GitHub 上
# File actionmailbox/lib/action_mailbox/base.rb, line 111 def bounce_now_with(message) inbound_email.bounced! message.deliver_now end
bounce_with(message) 連結
將指定的 message
加入佇列以進行遞送,並將收件郵件的狀態更改為 :bounced
(已退信)。
來源:顯示 | 在 GitHub 上
# File actionmailbox/lib/action_mailbox/base.rb, line 105 def bounce_with(message) inbound_email.bounced! message.deliver_later end
process() 連結
來源:顯示 | 在 GitHub 上
# File actionmailbox/lib/action_mailbox/base.rb, line 96 def process # Override in subclasses end