我正在使用ActionMailbox在Rails 6上开发内部支出应用程序,以替换Excel电子表格。由于我们现在的许多收据都采用电子邮件形式(例如机票),因此想法是用户将能够简单地将收据转发到应用程序,并且收据将自动与费用条目关联。
我正在将ActionMailbox与Mailgun一起用作电子邮件接收者。正如Gorails Pro教程所建议的那样,我已经使用localtunnel将我的应用暴露给了一般的Internet。我已经使用Mailgun的工具将测试电子邮件发送到我的应用程序。
我的帖子地址是:
https://xxxxxxxx.localtunnel.me/rails/action_mailbox/mailgun/inbound_emails/mime
但是,我遇到了一个问题,其中来自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)
我的配置是:config / routes.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
config / application.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
config / environments / development.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
app / mailboxes / application_mailbox.rb
class ApplicationMailbox < ActionMailbox::Base
routing :all => :receipt
end
app / mailboxes / receipt_mailbox.rb
class ReceiptMailbox < ApplicationMailbox
# mail => Mail object
# inbound_email => ActionMailboxEmail record
def process
end
end
嗯,原来这是最愚蠢的错误。在config / application.rb中,最终配置错误。我需要将:mail_gun
替换为:mailgun
:
# Set the ActionMailbox ingress here for now.
config.action_mailbox.ingress = :mailgun
不幸的是,Rails错误消息在这里不是很有帮助。
为了提供更多信息,ActionMailbox为不同的电子邮件处理器定义了许多不同的路由。因此,电子邮件处理器将收到的电子邮件发布到的URL定义了要使用的控制器。收到电子邮件后,Rails会检查已将电子邮件发布到的URL是否与配置中设置的ingress
类匹配。如果没有,则会出现我看到的错误。