跳至内容 跳至搜索

接收来自 SendGrid 的入站电子邮件。需要一个包含完整 RFC 822 消息的 email 参数。

使用 HTTP 基本身份验证来验证请求。用户名始终是 actionmailbox,密码从应用程序的加密凭据或环境变量中读取。请参阅下面的“用法”部分。

请注意,基本身份验证在未加密的 HTTP 上不安全。拦截 SendGrid 入站接口明文请求的攻击者可以得知其密码。您应该仅在 HTTPS 上使用 SendGrid 入站接口。

返回值

  • 204 No Content:如果入站电子邮件已成功记录并排队以路由到相应的邮箱

  • 如果请求的签名无法验证,则返回 401 Unauthorized

  • 如果 Action Mailbox 未配置为接受来自 SendGrid 的入站电子邮件,则返回 404 Not Found

  • 如果请求缺少必需的 email 参数,则返回 422 Unprocessable Entity

  • 如果入站接口密码未配置,或者 Active Record 数据库、Active Storage 服务或 Active Job 后端配置错误或不可用,则返回 500 Server Error

用法

  1. 告诉 Action Mailbox 接受来自 SendGrid 的电子邮件

    # config/environments/production.rb
    config.action_mailbox.ingress = :sendgrid
    
  2. 生成一个强密码,Action Mailbox 可用于向 SendGrid 入站接口进行身份验证。

    使用 bin/rails credentials:edit 将密码添加到应用程序的加密凭据中,位于 action_mailbox.ingress_password 下,Action Mailbox 会自动找到它。

    action_mailbox:
      ingress_password: ...

    或者,在 RAILS_INBOUND_EMAIL_PASSWORD 环境变量中提供密码。

  3. 配置 SendGrid 入站解析 将入站电子邮件转发到 /rails/action_mailbox/sendgrid/inbound_emails,用户名设置为 actionmailbox,密码设置为您之前生成的密码。如果您的应用程序位于 https://example.com,您将使用以下完全限定的 URL 配置 SendGrid:

    https://actionmailbox:PASSWORD@example.com/rails/action_mailbox/sendgrid/inbound_emails

    注意: 在配置 SendGrid 入站解析 Webhook 时,请务必勾选标有*“Post the raw, full MIME message.”*(发布原始、完整的 MIME 消息)的复选框。Action Mailbox 需要原始 MIME 消息才能正常工作。

方法
C

实例公共方法

create()

# File actionmailbox/app/controllers/action_mailbox/ingresses/sendgrid/inbound_emails_controller.rb, line 51
def create
  ActionMailbox::InboundEmail.create_and_extract_message_id! mail
rescue JSON::ParserError => error
  logger.error error.message
  head ActionDispatch::Constants::UNPROCESSABLE_CONTENT
end