跳至内容 跳至搜索
方法
C
R

实例公共方法

create_inbound_email_from_fixture(fixture_name, status: :processing)

使用 test/fixtures/files/fixture_name 中以 fixture_name 为引用的 message/rfc822 格式的 eml 固定装置创建 InboundEmail 记录。

# File actionmailbox/lib/action_mailbox/test_helper.rb, line 9
def create_inbound_email_from_fixture(fixture_name, status: :processing)
  create_inbound_email_from_source file_fixture(fixture_name).read, status: status
end

create_inbound_email_from_mail(status: :processing, **mail_options, &block)

通过选项或代码块创建 InboundEmail

选项

  • :status - 为创建的 InboundEmail 设置的 status。有关可能的 状态,请参阅其文档。

创建简单电子邮件

当你只需要设置基本字段(如 fromtosubjectbody)时,你可以将它们直接作为选项传递。

create_inbound_email_from_mail(from: "[email protected]", subject: "Hello!")

创建多部分电子邮件

当你需要创建更复杂的电子邮件(如包含纯文本版本和 HTML 版本的多部分电子邮件)时,你可以传递代码块。

create_inbound_email_from_mail do
  to "David Heinemeier Hansson <[email protected]>"
  from "Bilbo Baggins <[email protected]>"
  subject "Come down to the Shire!"

  text_part do
    body "Please join us for a party at Bag End"
  end

  html_part do
    body "<h1>Please join us for a party at Bag End</h1>"
  end
end

Mail.new 一样,你也可以使用代码块参数来定义消息的各个部分

create_inbound_email_from_mail do |mail|
  mail.to "David Heinemeier Hansson <[email protected]>"
  mail.from "Bilbo Baggins <[email protected]>"
  mail.subject "Come down to the Shire!"

  mail.text_part do |part|
    part.body "Please join us for a party at Bag End"
  end

  mail.html_part do |part|
    part.body "<h1>Please join us for a party at Bag End</h1>"
  end
end
# File actionmailbox/lib/action_mailbox/test_helper.rb, line 63
def create_inbound_email_from_mail(status: :processing, **mail_options, &block)
  mail = Mail.new(mail_options, &block)
  # Bcc header is not encoded by default
  mail[:bcc].include_in_headers = true if mail[:bcc]

  create_inbound_email_from_source mail.to_s, status: status
end

create_inbound_email_from_source(source, status: :processing)

使用原始 rfc822 source 作为文本创建 InboundEmail

# File actionmailbox/lib/action_mailbox/test_helper.rb, line 72
def create_inbound_email_from_source(source, status: :processing)
  ActionMailbox::InboundEmail.create_and_extract_message_id! source, status: status
end

receive_inbound_email_from_fixture(*args)

使用与 create_inbound_email_from_fixture 相同的参数从固定装置创建 InboundEmail,并立即将其路由到处理。

# File actionmailbox/lib/action_mailbox/test_helper.rb, line 79
def receive_inbound_email_from_fixture(*args)
  create_inbound_email_from_fixture(*args).tap(&:route)
end

receive_inbound_email_from_mail(**kwargs, &block)

使用与 create_inbound_email_from_mail 相同的选项或代码块创建 InboundEmail,然后立即将其路由到处理。

# File actionmailbox/lib/action_mailbox/test_helper.rb, line 85
def receive_inbound_email_from_mail(**kwargs, &block)
  create_inbound_email_from_mail(**kwargs, &block).tap(&:route)
end

receive_inbound_email_from_source(*args)

使用与 create_inbound_email_from_source 相同的参数创建 InboundEmail,并立即将其路由到处理。

# File actionmailbox/lib/action_mailbox/test_helper.rb, line 91
def receive_inbound_email_from_source(*args)
  create_inbound_email_from_source(*args).tap(&:route)
end