鉴于request
对象在ActionMailer
中无效,是否有办法检测当前URL并为自定义设计邮件控制器设置from
选项?
所以这就是我目前在application_controller.rb
中所拥有的:
before_filter :images, :hide_sidebar, :global_vars, :set_mailer_host
before_action :sidebar_menu
def set_mailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
@host = request.env['HTTP_HOST'] unless request.env['HTTP_HOST'].include? 'localhost'
end
Devise Mailer自定义类:
class DeviseMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
def confirmation_instructions(record, token, opts={})
if record.events.present?
name = record.events.last.name
@event = name
else
name = "#{record.first_name},"
end
opts[:from] = "noreply@#{@host}" if @host
opts[:subject] ="#{name} Registration - Confirmation Required"
super
end
end
同样在Devise.rb中,我设置如下:
config.mailer = 'DeviseMailer'
我不知道为什么我仍然会获取默认域而不是当前的网址。
由于我有多个url指向同一个应用程序,有没有办法检测到这个并配置Devise Mailers?
答案 0 :(得分:0)
为什么您不会将主机作为参数传递给您的邮件程序?
def confirmation_instructions(record, token, opts={}, host)
...
end
DeviseMailer.confirmation_instructions(..., @host).deliver