我有一个CloudFront CDN服务于我的rails资产。当查看通过Web浏览器提供的资产来源时,它会正确指向我的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
参数。由于
:host
通常在整个应用程序中是一致的,因此您可以在config/application.rb
中进行全局配置。>应该是:
config.action_mailer.default_url_options = { host: Figaro.env.cloudfront_endpoint }
注意:请确保您在邮件视图/模板中使用
image_url
而不是image_path