Action Mailbox Base¶ ↑
所有应用程序邮箱的基类。不打算直接继承。请继承 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
Attributes
| [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