Rails 6 ActionMailbox 与 Mailgun 返回 404

cpt*_*ter 3 ruby mailgun ruby-on-rails-6

我正在 Rails 6 上使用 ActionMailbox 开发一个内部费用应用程序来替换 Excel 电子表格。由于我们的许多收据现在采用电子邮件形式(例如机票),因此用户只需将收据转发到应用程序,它就会自动与费用条目相关联。

我使用 ActionMailbox 和 Mailgun 作为电子邮件接收器。正如 Gorails Pro 教程所建议的,我已使用 localtunnel 将我的应用程序公开到通用互联网。我已经使用 Mailgun 的功能向我的应用程序发送了一封测试电子邮件。

我的邮寄地址是:

https://xxxxxxxx.localtunnel.me/rails/action_mailbox/mailgun/inbound_emails/mime
Run Code Online (Sandbox Code Playgroud)

但是,我遇到了一个问题,即来自 Mailgun 的传入电子邮件未得到正确处理,而是返回 404 错误。Rails 日志显示以 POST 形式接收的消息。日志中的最后两个条目是:

2019-10-15T07:50:07.646Z 10260 TID-gn609ivg8 INFO: Filter chain halted as :ensure_configured rendered or redirected
2019-10-15T07:50:07.646Z 10260 TID-gn609ivg8 INFO: Completed 404 Not Found in 0ms (ActiveRecord: 0.0ms | Allocations: 144)
Run Code Online (Sandbox Code Playgroud)

我的配置是: config/routes.rb

https://xxxxxxxx.localtunnel.me/rails/action_mailbox/mailgun/inbound_emails/mime
Run Code Online (Sandbox Code Playgroud)

配置/应用程序.rb

2019-10-15T07:50:07.646Z 10260 TID-gn609ivg8 INFO: Filter chain halted as :ensure_configured rendered or redirected
2019-10-15T07:50:07.646Z 10260 TID-gn609ivg8 INFO: Completed 404 Not Found in 0ms (ActiveRecord: 0.0ms | Allocations: 144)
Run Code Online (Sandbox Code Playgroud)

配置/环境/development.rb

Rails.application.routes.draw do
  devise_for :users
  resources :categories

  resources :expense_claims do
    get 'export_excel', on: :member
    post 'barclay_csv_import', on: :collection
  end

  resources :expense_entries

  root 'expense_claims#index'

  # Enable the sidekiq console.
  require 'sidekiq/web'
  mount Sidekiq::Web => '/sidekiq'
end
Run Code Online (Sandbox Code Playgroud)

应用程序/邮箱/application_mailbox.rb

module Expenses
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.

    # Set the ActionMailbox ingress here for now.
    config.action_mailbox.ingress = :mail_gun
  end
end
Run Code Online (Sandbox Code Playgroud)

应用程序/邮箱/receipt_mailbox.rb

Rails.application.configure do

  [... lot of stuff removed as not relevant]
  # Settings specified here will take precedence over those in config/application.rb.
  config.action_mailer.default_url_options = { host: '0.0.0.0', port: 3000 }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = { address: '0.0.0.0', port: 1025 }

  # Set the active job queue adapter to Sidekiq/Redis
  # config.active_job.queue_adapter = :sidekiq
  # Alternatively, when debugging, you can set to in-line (or :async)
  config.active_job.queue_adapter = :inline

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.perform_caching = false


  # Set so we can test Devise self registration
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

  # Allow traffic from localtunnel
  config.hosts << 'xxxxxx.localtunnel.me'
end

Run Code Online (Sandbox Code Playgroud)

cpt*_*ter 6

事实证明这是最愚蠢的错误。在config/application.rb中,最终配置是错误的。我需要替换:mail_gun:mailgun

    # Set the ActionMailbox ingress here for now.
    config.action_mailbox.ingress = :mailgun
Run Code Online (Sandbox Code Playgroud)

不幸的是,Rails 错误消息在这里没有多大帮助。

为了提供更多信息,ActionMailbox 为不同的电子邮件处理器定义了许多不同的路由。因此,电子邮件处理器将传入电子邮件发布到的 URL 定义了要使用的控制器。收到电子邮件后,Rails 会检查电子邮件发布到的 URL 是否与ingress配置中设置的类匹配。如果没有,您会收到我看到的错误。