config.action_mailer.asset_host 不提供来自云端 CDN 的图像

config.action_mailer.asset_host not serving images from cloud front CDN

我有一个 CloudFront CDN 服务于我的 rails 资产。当通过网络浏览器查看资产来源时,它正确指向我的 CDN。但是,Action Mailer 在为其资产提供服务时并未使用此路径。

为什么会这样?

这是我的 environments/production.rb 文件:

 config.action_mailer.perform_caching = false
  config.action_mailer.default_url_options = { host: 'localhost' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.smtp_settings = {
      :address => "email-smtp.us-east-1.amazonaws.com",
      :port => 587,
      :user_name => Figaro.env.smtp_username, #Your SMTP user
      :password => Figaro.env.smtp_password, #Your SMTP password
      :authentication => :login,
      :enable_starttls_auto => true
  }
  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  config.action_controller.asset_host = Figaro.env.cloudfront_endpoint
  config.action_mailer.asset_host = Figaro.env.cloudfront_endpoint

您似乎没有为 config.action_mailer.default_url_options 设置正确的值。

This is what the docs says:

Unlike controllers, the mailer instance doesn't have any context about the incoming request so you'll need to provide the :host parameter yourself.

As the :host usually is consistent across the application you can configure it globally in config/application.rb

应该是:

config.action_mailer.default_url_options = { host: Figaro.env.cloudfront_endpoint }

注意:确保您在邮件中使用 image_url 而不是 image_path views/templates