2017-07-04 108 views
0

我在我的模型中有两个电子邮件字段:电子邮件和:representative_email我想发送确认说明电子邮件:representative_email(如果存在或不存在)而不是:电子邮件我该怎么做有效率的。我仍然想用我的电子邮件字段登录。设计其他电子邮件的确认说明

回答

0

我需要邮件

class DeviseCustomMailer < 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' 
    def headers_for(action, opts) 
    headers = { 
     subject: subject_for(action), 
     to: resource.notification_email_for(action), #I changed this line 
     from: mailer_sender(devise_mapping), 
     reply_to: mailer_reply_to(devise_mapping), 
     template_path: template_paths, 
     template_name: action 
    }.merge(opts) 

    @email = headers[:to] 
    headers 
    end 
end 

内,在我的user.rb

def notification_email_for(action) 
    (self.company? && self.authorised_representative && action == :confirmation_instructions) ? self.representative_email : self.email 
end 

和我的配置/初始化/ devise.rb

config.mailer = 'DeviseCustomMailer' 
覆盖headers_for方法色器件

就是这样

1

您可以创建一个自定义邮件程序see how to do that,并在其中设置要发送的电子邮件。 这是我能想到的最干净的解决方案,未来如果您想改变任何东西,只能在一个地方改变它。

+0

yes I需要编写自定义邮件,但在我的答案中有更多描述。 –

+0

真棒,感谢分享,其他人会从中受益。 –