在设计电子邮件主题中添加动态值

时间:2013-05-24 20:08:12

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

好的我看过很多关于自定义设计电子邮件主题的讨论,但似乎没有解决我想要的问题。目前,我的确认电子邮件主题为“确认您的Qitch.com帐户”。我想自定义此电子邮件主题,并在其中添加用户名称的动态值,以便在用户 ALEX 注册帐户时,他应该获得一个包含主题的电子邮件地址,欢迎ALEX,确认您的Qitch.com帐户。我怎样才能在设计中实现这一目标?

devise.en.yml

mailer:
  confirmation_instructions:
    subject: 'Confirm your Qitch.com account'
  reset_password_instructions:
    subject: 'Reset your Qitch.com password'
  unlock_instructions:
    subject: 'Unlock your Qitch.com account'

最后,如何在回复地址或地址中添加名称,目前收到邮件时,发件人说:no-reply@qitch.com有没有办法可以将其自定义为Qitch < / p>

由于

5 个答案:

答案 0 :(得分:14)

我看到答案都不够干净,所以我想在这里做一个简短的总结。

  1. 首先,您必须告诉Devise您将通过以下方式覆盖其原始邮件程序方法:
  2. <强>配置/初始化/ devise.rb

    config.mailer = 'MyOverriddenMailer'

    1. 之后,您需要创建被覆盖的邮件程序类并覆盖您想要的任何方法:
    2. 应用/邮寄者/ my_overridden_​​mailer.rb

      class MyOverriddenMailer < Devise::Mailer
        helper :application # gives access to all helpers defined within `application_helper`.
        include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
        default template_path: 'devise/mailer' # to make sure that you mailer uses the devise views
      
        def confirmation_instructions(record, token, opts={})
          if record.name.present?
            opts[:subject] = "Welcome #{record.name}, confirm your Qitch.com account"
          else
            opts[:subject] = "Confirm your Qitch.com account"
          end
      
          super
        end
      
      end
      
      1. 请记住重新启动Rails服务器以应用更改! :)
      2. 注意

        • opts选项列表:subject,to,from,reply_to,template_path,template_name。
        • recordUser model
        • 的实例
        • 当然还有origin document

答案 1 :(得分:5)

设计助手hereHow To: Use custom mailer

class MyMailer < Devise::Mailer


   def confirmation_instructions(record, opts={})
    headers = {
        :subject => "Welcome  #{resource.name}, confirm your Qitch.com account"
    }
    super
  end

  def reset_password_instructions(record, opts={})
    headers = {
        :subject => "Welcome  #{resource.name}, reset your Qitch.com password"
    }
    super
  end

  def unlock_instructions(record, opts={})
    headers = {
        :subject => "Welcome  #{resource.name}, unlock your Qitch.com account"
    }
    super
  end

end

或者

class MyMailer < Devise::Mailer
...
...
private

 def headers_for(action)
  if action == :confirmation_instructions
    headers = {
      :subject => "Welcome  #{resource.name}, confirm your Qitch.com account"
    }
  elsif action == :reset_password_instructions
    headers = {
      :subject => "Welcome  #{resource.name}, reset your Qitch.com password"
    }
  else
    headers = {
      :subject => "Welcome  #{resource.name}, unlock your Qitch.com account"
        }
  end
 end
end

告诉设计使用你的邮件:

#config/initializers/devise.rb
config.mailer = "MyMailer"

注意:我还没有尝试过,但它们可能对我们有帮助,请更正我的答案,如果有错误可以编辑我的答案

答案 2 :(得分:4)

我正在使用devise (3.2.1)并且我已经实现了以下解决方案,但是要使用本地化修改from:字段:

# app/mailers/devise_mailer.rb
class DeviseMailer < Devise::Mailer

  def confirmation_instructions(record, token, opts={})
    custom_options(opts)
    super
  end

  def reset_password_instructions(record, token, opts={})
    custom_options(opts)
    super
  end

  def unlock_instructions(record, token, opts={})
    custom_options(opts)
    super
  end

  private

  def custom_options(opts)
    opts[:from] = I18n.t('devise.mailer.from', name: Tenancy.current_tenancy.name, mail: ENV['FROM_MAILER'] )
  end
end

然后我在我的语言环境文件中定义了消息

# config/locales/devise.es.yml
es:
  devise:
    mailer:
      from: "Portal de trabajo %{name} <%{mail}>"

要修改主题,它应该几乎相同:

  def confirmation_instructions(record, token, opts={})
    custom_options(opts, :confirmation_instructions)
    super
  end

  private

  def custom_options(opts, key)
    opts[:from] = I18n.t('subject', scope: [:devise, :mailer, key])
  end

# and in your locale file
es:
  devise:
    mailer:
      confirmation_instructions:
        subject: Instrucciones de confirmación

答案 3 :(得分:0)

headers_for插入例如在所有设计邮件的主题之前加上

# config/initializers/devise.rb

Devise.setup do |config|
  # ...
  config.mailer = 'DeviseMailer'
  # ...
end
# app/mailers/devise_mailer.rb

class DeviseMailer < Devise::Mailer
  def headers_for(action, opts)
    super.merge!({ subject: 'Hi ALEX! ' + subject_for(action) })
  end
end

答案 4 :(得分:-2)

Yo应该像这样创建一个ActionMailer:

class Sender < ActionMailer::Base
   default :from => "'Eventer' <dfghjk@gmail.com>"

   def send_recover(user, pw)
      mail(:to => user.email , :subject => "You have requested to change your password from Eventer") do |format|
             format.text { 
             render :text =>"Dear #{user.name}, 

                             **Body**

                             Regards."
            }
      end
   end
end

然后你应该这样从控制器调用它:

Sender.send_recover(@mail, current_user, @meetup).deliver

我希望它适合你!

此致