11

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

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

我做了以下事情:

  • 创建了一个带有自定义方法的自定义设计邮件程序。此方法使用参数调用 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”,我没有看到如何将标题传递到邮件程序中。我可以使用简单的设置或方法来设置电子邮件主题吗?

4

5 回答 5

21

请参阅设计助手

class AccountMailer < Devise::Mailer


   def confirmation_instructions(record, opts={})
    headers = {
        :subject => "Subject Here"
    }
    super
  end

end

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

并设置自己的主题

mailer:
    confirmation_instructions:
        subject: 'Confirmation instructions'
于 2013-06-08T19:42:38.073 回答
6

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

对于自定义主题:

  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'

于 2016-04-19T08:22:29.880 回答
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'
于 2019-08-17T19:20:07.027 回答
0

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

于 2013-06-18T18:15:01.687 回答
-1

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

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

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

Devise::Mailer.confirmation_instructions(user, {subject: "Custom Subject"}).deliver
于 2014-02-10T22:45:53.467 回答