从选项设计邮件定制域

时间:2014-02-17 11:37:35

标签: ruby-on-rails ruby devise ruby-on-rails-4

鉴于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?

1 个答案:

答案 0 :(得分:0)

为什么您不会将主机作为参数传递给您的邮件程序?

def confirmation_instructions(record, token, opts={}, host)
    ...
end

DeviseMailer.confirmation_instructions(..., @host).deliver