Ruby on rails 轨道3和x2B;设计:如何修改确认电子邮件的mailer方法以添加用户';第二个电子邮件地址

Ruby on rails 轨道3和x2B;设计:如何修改确认电子邮件的mailer方法以添加用户';第二个电子邮件地址,ruby-on-rails,devise,registration,Ruby On Rails,Devise,Registration,背景:在我们的应用程序中,我们经常让销售代表使用销售人员的计算机为客户进行设置(通常客户在我们设置他们时无法访问他们的电子邮件)。因此,我们正在考虑为销售代表的电子邮件地址在Desive注册表中添加一个字段,并使确认链接也指向该电子邮件地址 问题:是否有办法告诉Deave to bcc(或cc)将初始确认电子邮件(仅初始确认电子邮件)发送到新用户注册表中提供的(可选)“备份电子邮件”电子邮件地址 或者,是否有一种方法“禁用”确认电子邮件流程,但仅当在注册字段中输入特定代码时 我知道如何在设计登记

背景:在我们的应用程序中,我们经常让销售代表使用销售人员的计算机为客户进行设置(通常客户在我们设置他们时无法访问他们的电子邮件)。因此,我们正在考虑为销售代表的电子邮件地址在Desive注册表中添加一个字段,并使确认链接也指向该电子邮件地址

问题:是否有办法告诉Deave to bcc(或cc)将初始确认电子邮件(仅初始确认电子邮件)发送到新用户注册表中提供的(可选)“备份电子邮件”电子邮件地址

或者,是否有一种方法“禁用”确认电子邮件流程,但仅当在注册字段中输入特定代码时

我知道如何在设计登记表中添加另一个字段,但我不知道如何/在何处修改设计邮件代码,因此当确认电子邮件发送到“电子邮件”地址时,它也会发送到“备份电子邮件”地址(如果有,有时为空)


多亏了约翰尼·格拉斯

我确实
rails生成了mailer-CustomerUserMailer
并补充说

#config/initializers/devise.rb
config.mailer = "CustomUserMailer"
我的自定义邮件程序如下所示:

# app/mailers/customer_user_mailer.rb
class CustomUserMailer < Devise::Mailer
  def headers_for(action)
    headers = {
      :subject       => translate(devise_mapping, action),
      :from          => mailer_sender(devise_mapping),
      :to            => resource.email,
      :cc            => resource.backup_user_email(action),
      :template_path => template_paths
    }
  end
end
#app/mailers/customer_user_mailer.rb
类CustomUserMailertranslate(设计映射、操作),
:from=>mailer\u sender(设计映射),
:to=>resource.email,
:cc=>resource.backup\u user\u email(操作),
:template\u path=>template\u路径
}
结束
结束
然后,我将3个邮件模板从
views/designe/mailer
移动到
views/customer\u user\u mailer
(否则邮件是空的)


然后,我在我的
User
模型中添加了一个名为
backup\u User\u email()
的方法,该方法根据用户记录中的数据和操作返回“备份”电子邮件地址(如果有)。唯一的“诀窍”是,在测试
操作时,它不是
操作==“确认\u指令”
而是
操作==:确认\u指令
一种方法是在


以防有人通过谷歌来到这里——在最新版本的Deviate中,
header\u包含两个参数。因此,您的代码需要是:

class MyMailer < Devise::Mailer
  backup_email = "..."
  def headers_for(action, opts)
    headers = {
      :subject       => subject_for(action),
      :to            => resource.email,
      :from          => mailer_sender(devise_mapping),
      :bcc           => backup_email,
      :reply_to      => mailer_reply_to(devise_mapping),
      :template_path => template_paths,
      :template_name => action
    }.merge(opts)
  end
end
class MyMailersubject_for(动作),
:to=>resource.email,
:from=>mailer\u sender(设计映射),
:bcc=>备份\u电子邮件,
:reply_to=>mailer_reply_to(设计映射),
:template\u path=>template\u路径,
:template_name=>操作
}.合并(opts)
结束
结束

这可能不是最好的方法,但至少它避免了错误。

您可以使用更干净、更简单的代码

# app/mailers/my_mailer.rb
class MyMailer < Devise::Mailer
  def headers_for(action, opts)
    backup_email = "..."
    super.merge!({bcc: backup_email})
  end
end

# config/initializers/devise.rb
...
config.mailer = MyMailer
...
#app/mailers/my_mailer.rb
类MyMailer

将散列传递给
合并方法您可以添加或修改任何您想要的电子邮件标题。

您的答案对我很有用。非常感谢你

我有一个场景,其中我被要求定制设计: 1) 以密件抄送方式将注册确认电子邮件发送至不同的电子邮件,具体取决于环境。 2) 只有注册确认邮件才能将电子邮件添加到密件抄送中

为了实现这一点,我比较了action参数的值,如下面的代码片段所示:

def headers_for(action)
  if action == :confirmation_instructions
    if Rails.env.production?
        recipient_email = "user1@example.com"
    else
        recipient_email = "user2@example.com"
    end

    headers = {
      :subject => translate(devise_mapping, action),
      :from => mailer_sender(devise_mapping),
      :to => resource.email,
      :bcc => recipient_email,
      :template_path => template_paths
    }
  else
    super
  end
end

一个问题。。。我将在哪里保存新文件?这是非常有用的信息。备份电子邮件来自每个用户的注册表单上的数据,希望我们可以使用类似的内容:bcc=>resource.backup\u email。实际上,您必须创建一个邮件程序,但要将其改为
designe::mailer
的子类。非常感谢。我在原始问题的末尾记录了这些更改,因为我还需要将mailer erb模板移动到Desive的上一个版本中的新视图。这个答案行不通。我将提交一个新的。如何在加载资源的同时做到这一点;i、 e.我想将“发件人”字段设置为resource.invested\u by.full\u name。当我这样做时,我会收到没有正文的电子邮件。所有的标题都很好。我看不出“模板路径”是如何定义的——有没有可能因为我使用的是生成的视图,它们就不起作用了?不管怎样——结果是,如果我将:template\u path更改为:
:template\u path=>“designe/mailer”
我可以使用我已经创建的模板。
# app/mailers/my_mailer.rb
class MyMailer < Devise::Mailer
  def headers_for(action, opts)
    backup_email = "..."
    super.merge!({bcc: backup_email})
  end
end

# config/initializers/devise.rb
...
config.mailer = MyMailer
...
def headers_for(action)
  if action == :confirmation_instructions
    if Rails.env.production?
        recipient_email = "user1@example.com"
    else
        recipient_email = "user2@example.com"
    end

    headers = {
      :subject => translate(devise_mapping, action),
      :from => mailer_sender(devise_mapping),
      :to => resource.email,
      :bcc => recipient_email,
      :template_path => template_paths
    }
  else
    super
  end
end