我的模型中有两个电子邮件字段:email和:representative_email我想发送确认说明电子邮件:representative_email(如果存在或不是nil)而不是:email我怎样才能有效地执行此操作。我仍然想用我的:电子邮件字段登录。
答案 0 :(得分:1)
您可以创建自定义邮件程序see how to do that,并在其中设置要发送的电子邮件。 这是我能想到的最干净的解决方案,将来如果你想改变任何东西,你只能在一个地方改变它。
答案 1 :(得分:0)
我需要覆盖邮件程序中设计方法的headers_for方法
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
和我的config / initializers / devise.rb
config.mailer = 'DeviseCustomMailer'
就是这样