rails 3 + devise:如何修改邮件方法确认电子邮件以添加用户的第二个电子邮件地址 [英] rails 3 + devise: how to modify the mailer method for confirmation emails to add user's second email address

查看:139
本文介绍了rails 3 + devise:如何修改邮件方法确认电子邮件以添加用户的第二个电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Background: In our app, we often have a sales rep do the setup for our customer using the salesperson's computer (often customers don't have access to their email at the time we set them up). So we're thinking to add a field to the devise registration form for the sales rep's email address and have the confirm link ALSO go to that email address.

问题:有没有办法告知设定密码(或cc)初始确认电子邮件(仅限初始确认电子邮件)到(备选)backup_email电子邮件地址提供在新的用户注册表上?

Question: Is there a way to tell devise to bcc (or cc) the initial confirmation email (only the initial confirmation email) to an (optional) "backup_email" email address that is also provided on the new user registration form?

或者,是否有一种方法来禁用确认电子邮件过程,但只有当某个代码输入注册字段时?

Alternatively, is there a way to 'disable' the confirmation email process but ONLY when a certain code is entered into the registration field?

我知道如何向设计注册表单添加另一个字段,但是我看不到如何/在哪里修改设计邮件程序代码,所以当发送确认电子邮件时到电子邮件地址,它也转到backup_email地址(如果有的话,有时它是空白的)。

I know how to add another field to the devise registration form, but I don't see how/where to modify the devise mailer code so when a confirmation email is sent to the "email" address it ALSO goes to the "backup_email" address (if any, sometimes it's blank).

感谢Johnny Grass!

Thanks to Johnny Grass!

我做了 rails生成邮件卡斯托merUserMailer
并添加

I did rails generate mailer CustomerUserMailer and added

#config/initializers/devise.rb
config.mailer = "CustomUserMailer"

我的自定义邮件程序看起来像:

my custom mailer looks like:

# 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

然后我移动了3邮件模板FROM views / devise / mailer to views / customer_user_mailer (否则电子邮件为空)

Then I moved the 3 mailer templates FROM views/devise/mailer to views/customer_user_mailer (otherwise the emails are empty)

然后我添加了一个方法到我的用户模型称为 backup_user_email()根据用户记录和操作中的数据返回备份电子邮件地址(如果有)。唯一的窍门就是在测试动作时,它不是 action ==confirmation_instructions它是 action ==:confirmation_instructions

Then I added a method to my User model called backup_user_email() that returns the 'backup' email address (if any) based on the data in the user record and the action. The only "trick" there is that when testing the action it is not action == "confirmation_instructions" it is action == :confirmation_instructions.

推荐答案

要覆盖 headers_for 操作 > Devise :: Mailer

One way to do it would be to override the headers_for action in Devise::Mailer

class MyMailer < Devise::Mailer
  backup_email = "..."
  def headers_for(action)
    headers = {
     :subject       => translate(devise_mapping, action),
     :from          => mailer_sender(devise_mapping),
     :to            => resource.email,
     :bcc           => backup_email
     :template_path => template_paths
  }
end

并告诉设计使用你的邮件程序: p>

And tell devise to use your mailer:

#config/initializers/devise.rb
config.mailer = "MyMailer"

这篇关于rails 3 + devise:如何修改邮件方法确认电子邮件以添加用户的第二个电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆