跳至內容 跳至搜尋
方法
C
R

實例公開方法

create_inbound_email_from_fixture(fixture_name, status: :processing)

使用位於 `test/fixtures/files/fixture_name` 中,格式為 message/rfc822 的 eml 範例檔,建立一個以 `fixture_name` 參照的 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`。可能的狀態,請參閱其說明文件。

建立簡單的電子郵件

當您只需要設定基本欄位,例如 `from`、`to`、`subject` 和 `body` 時,您可以直接將它們作為選項傳遞。

create_inbound_email_from_mail(from: "david@loudthinking.com", subject: "Hello!")

建立多部分電子郵件

當您需要建立更複雜的電子郵件時,例如包含純文字版本和 HTML 版本的多部分電子郵件,您可以傳遞一個區塊。

create_inbound_email_from_mail do
  to "David Heinemeier Hansson <david@loudthinking.com>"
  from "Bilbo Baggins <bilbo@bagend.com>"
  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 <david@loudthinking.com>"
  mail.from "Bilbo Baggins <bilbo@bagend.com>"
  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