【问题标题】:Add dynamic value in devise email subject在设计电子邮件主题中添加动态值
【发布时间】:2013-05-20 12:30:09
【问题描述】:

好的,我已经看到很多关于自定义设计电子邮件主题的讨论,但似乎没有一个能解决我想要的问题。目前我的确认邮件主题为“Confirm your Qitch.com account”。我想自定义这个电子邮件主题并在其中添加用户名的动态值,这样如果用户 ALEX 注册了一个帐户,他应该得到一个主题为 Welcome 的电子邮件地址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

谢谢

【问题讨论】:

  • 截至 2014 年 10 月 23 日,这将是一个更好的解决方案,因为它可能是最简单的解决方案,最重要的是确实有效stackoverflow.com/a/21344142/102133

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我发现没有足够清晰的答案,所以我想在这里做一个简短的总结。

    1. 首先,您必须告诉Devise 您将通过以下方式覆盖其原始邮件方法:

    config/initializers/devise.rb

    config.mailer = 'MyOverriddenMailer'

    1. 之后,您需要创建重写的邮件程序类并重写您想要的任何方法,如下所示:

    app/mailers/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 服务器以应用更改! :)

    注意

    • opts 选项列表:主题、收件人、发件人、回复收件人、模板路径、模板名称。
    • recordUser 模型的实例
    • 当然还有origin document

    【讨论】:

    • 重置服务器以加载初始化程序中的更改后,这对我有用。
    【解决方案3】:

    我正在使用 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
    

    【讨论】:

      【解决方案4】:

      你应该创建一个像这样的 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
      

      我希望它对你有用!

      问候

      【讨论】:

      • 这看起来像是一个关于在 Rails 中使用邮件程序的通用示例,但这里的问题是关于覆盖设计特定功能的最佳方法
      【解决方案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"
      

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

      【讨论】:

      • 我应该把这个自定义邮件类放在哪里?谢谢
      • 已更正:app/mailers/my_mailer.rb @Joseph:你的问题解决了吗?
      • 对于设计 3.2+,您需要在 record 之后使用 token 参数并使用 opts[:subject] = 'your subject' 设置自定义主题。
      猜你喜欢
      • 2019-01-09
      • 1970-01-01
      • 2014-06-26
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 2013-06-04
      • 2021-03-14
      相关资源
      最近更新 更多