我一定不明白关于电子邮件的一些琐碎事情,但是 defaul_url_options 中的主机是做什么的?需要 smtp 设置对我来说配置电子邮件的发送方式是有意义的,但 default_url_options 与此有什么关系?
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = '<your heroku app>.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
最佳答案
default_url_options
设置对于构造 很有用电子邮件模板中的链接 URL .通常,:host
,即 Web 服务器的完全限定名称,需要使用此配置选项进行设置。它与发送电子邮件无关,它只配置在电子邮件中显示链接。
Rails Guides 中很好地记录了设置它的必要性。还有 ActionMailer::Base
sources :
URLs can be generated in mailer views using url_for or named routes. Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request, so you'll need to provide all of the details needed to generate a URL.
When using url_for you'll need to provide the
:host
,:controller
, and:action:
<%= url_for(host: "example.com", controller: "welcome", action: "greeting") %>
When using named routes you only need to supply the
:host
<%= users_url(host: "example.com") %>
因此,为了改写文档,在网页中,当前 Web 服务器的名称(用于绝对链接)取自传入的请求信息。但是您在呈现电子邮件时没有此信息可用(没有请求),这就是您必须手动提供它的原因,以便电子邮件中的链接正常工作。
关于ruby-on-rails - 什么是 ActionMailer default_url_options?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43961548/