如何为 Devise Mailer 电子邮件设置自定义密件抄送/收件人字段?

Ric*_*ard 1 ruby-on-rails devise

我想在通过 Devise 发送的所有电子邮件的 to/bcc 标头中包含我用户的名字和姓氏。

我看了这个https://github.com/plataformatec/devise/blob/master/lib/devise/mailers/helpers.rb#L28

似乎 Devise 也像@email在模板中一样分配了 to 字段。我将如何覆盖标头,以便使用 to/bcc 标头发送电子邮件"FirstName LastName <email@example.com>"

MZa*_*oza 7

我会先看看How-To:-Use-custom-mailer

要使用自定义邮件程序,请创建一个扩展 Devise::Mailer 的类,如下所示:

class MyMailer < 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 your mailer uses the devise views
end
Run Code Online (Sandbox Code Playgroud)

然后,在您的 config/initializers/devise.rb 中,设置config.mailer"MyMailer".

def confirmation_instructions(record, token, opts={})
  opts[:to] = 'fname lname<email@example.com>'
  opts[:bcc] = 'fname lname<email@example.com>'
  super
end
Run Code Online (Sandbox Code Playgroud)

我希望这会有所帮助并使您朝着正确的方向前进