【问题标题】:How do I set a custom email subject in a custom Devise email?如何在自定义设计电子邮件中设置自定义电子邮件主题?
【发布时间】:2013-06-04 21:01:17
【问题描述】:

我在 Rails 3 应用程序中使用 Devise 创建帐户。我有不同类型的用户,所以我想根据用户类型发送自定义密码恢复电子邮件。

我能够发送自定义电子邮件,但我还没有找到在该电子邮件上设置自定义标题的方法。我对设置电子邮件的主题特别感兴趣。

我做了以下事情:

  • 创建了一个自定义设计邮件程序,其中包含自定义方法。此方法使用参数调用 devise_mail。在这种情况下,自定义邮件程序称为“reset_partner_instructions”。我可以调用这个邮件程序并成功地从我的用户模型发送一封电子邮件。
  • 创建了一个自定义电子邮件视图模板,该模板已成功从devise_mail 调用。

我的自定义邮件如下所示:

class AccountMailer < Devise::Mailer
  helper :application # gives access to all helpers defined within application_helper.
  def reset_partner_instructions(record, opts={})
    devise_mail(record, :reset_partner_instructions, opts)
  end
end

问题在于电子邮件的主题始终是“重置合作伙伴说明”。我相信 Devise 是根据邮件模板的名称生成此标​​题。

在本教程https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer中,他们调用了以下代码:

def confirmation_instructions(record, opts={})
  headers["Custom-header"] = "Bar"
  super
end

由于我直接调用“devise_mail”,我没有看到如何将标头传递到邮件程序。有没有简单的设置或方法可以用来设置邮件主题?

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    devise helper

    class AccountMailer < Devise::Mailer
    
    
       def confirmation_instructions(record, opts={})
        headers = {
            :subject => "Subject Here"
        }
        super
      end
    
    end
    

    或者你可以在 intilizer 目录的devise.en.yml 文件中更改它

    并设置自己的主题

    mailer:
        confirmation_instructions:
            subject: 'Confirmation instructions'
    

    【讨论】:

    • 感谢您的帮助!我最终只是创建了一个自定义邮件。我觉得这是一个更清洁的解决方案,我不必重写 Devise 所做的任何事情。
    • 但是如何将变量传递给语言环境?就像我想在主题中使用名字一样。
    • @AmmarShah 看看here
    【解决方案2】:

    这是一个很老的问题,它可能对你还是其他人有帮助,

    自定义主题:

    1. 创建文件config/locales/devise.en.yml
    2. 添加如下所示的内容,确保像在database.yml文件中一样使用2个空格正确缩进

      en: devise: mailer: confirmation_instructions: subject: 'Verification subject here' reset_password_instructions: subject: 'Reset password subject here'

    【讨论】:

      【解决方案3】:

      这是一个老问题,但仍然出现在搜索的顶部,我可以通过设置 opts[:subject] 而不是设置标题来解决这个问题:

      # https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer
      class DeviseMailer < Devise::Mailer   
        helper :application
        include Devise::Controllers::UrlHelpers
        default template_path: 'devise/mailer'
      
        def confirmation_instructions(record, token, opts={})
          opts[:subject] = ...
          opts[:from] = ...
          opts[:reply_to] = ...
          super
        end
      end
      

      devise.rb:

        config.mailer = 'DeviseMailer'
      

      【讨论】:

        【解决方案4】:

        我意识到我应该只使用 ActionMailer 来完成这项任务。 Devise 没有给我额外的功能,因为我试图生成一个自定义邮件,我可以在 Devise 之外完成。

        【讨论】:

          【解决方案5】:

          无需设置自定义邮件程序或覆盖 Devise 的 confirmation_instructions 方法。

          Devise 让您将可选参数传递给 confirmation_instructions 方法,该方法将与 Devise 的默认选项合并(opts 哈希被传递给 Devise 的 headers_for 帮助器)。

          因此您可以使用以下代码发送带有自定义主题的确认说明:

          Devise::Mailer.confirmation_instructions(user, {subject: "Custom Subject"}).deliver
          

          【讨论】:

          • 当我使用你的解决方案时,它根本不起作用,当我设置 #template_path: "/app/views/shared/users/mailer", # template_name: "mytest.html.erb" ,rails 抛出此错误:缺少带有“mailer”的模板/app/views/shared/users/mailer/mytest.html.erb。在以下位置搜索:*“/app/views/shared/users/mailer”。虽然文件已经存在了。
          猜你喜欢
          • 1970-01-01
          • 2012-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-06
          • 2018-01-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多