【问题标题】:config.action_mailer.asset_host not serving images from cloud front CDNconfig.action_mailer.asset_host 不提供来自云端 CDN 的图像
【发布时间】:2020-03-04 12:38:26
【问题描述】:

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

为什么会这样?

这是我的环境/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 = { host: 'localhost' }

标签: ruby-on-rails ruby ruby-on-rails-5 amazon-cloudfront actionmailer


【解决方案1】:

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

This is what the docs says:

与控制器不同,邮件程序实例没有关于传入请求的任何上下文,因此您需要自己提供 :host 参数。

由于:host 通常在整个应用程序中是一致的,您可以在config/application.rb 中全局配置它

应该是:

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

注意:确保您在邮件视图/模板中使用 image_url 而不是 image_path

【讨论】:

  • 我在 image_url 标签上本地覆盖了主机。你的回答让我得出了这个结论。
  • 如果存在也关注config/application.yml
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-18
  • 2022-07-29
  • 2020-01-15
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多