跳至内容 跳至搜索

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_processingafter_processingaround_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)

# File actionmailbox/lib/action_mailbox/base.rb, line 79
def initialize(inbound_email)
  @inbound_email = inbound_email
end

receive(inbound_email)

# 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

# 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

# File actionmailbox/lib/action_mailbox/base.rb, line 105
def bounce_with(message)
  inbound_email.bounced!
  message.deliver_later
end

process()

# File actionmailbox/lib/action_mailbox/base.rb, line 96
def process
  # Override in subclasses
end