我希望我的应用邀请来自邀请者,而不是系统电子邮件地址。如何从devise.rb覆盖config.mailer_sender?
我在邮件中有这个并且已经确认它已被调用,但它不会覆盖:from
。注意:它是一个私有方法,我把它作为一个没有效果的公共方法。
private
def headers_for(action)
if action == :invitation_instructions
headers = {
:subject => "#{resource.invited_by.full_name} has invited you to join iTourSmart",
:from => resource.invited_by.email,
:to => resource.email,
:template_path => template_paths
}
else
headers = {
:from => mailer_sender(devise_mapping),
:to => resource.email,
:template_path => template_paths
}
end
if resource.respond_to?(:headers_for)
headers.merge!(resource.headers_for(action))
end
unless headers.key?(:reply_to)
headers[:reply_to] = headers[:from]
end
headers
end
答案 0 :(得分:5)
没有任何黑客/猴子补丁的更好的解决方案将是: 例如,在您的模型中:
def invite_and_notificate_member user_email
member = User.invite!({ email: user_email }, self.account_user) do |u|
u.skip_invitation = true
end
notificate_by_invitation!(member)
end
def notificate_by_invitation! member
UserMailer.invited_user_instructions(member, self.account_user, self.name).deliver
end
在邮件中:
def invited_user_instructions(user, current_user, sa)
@user = user
@current_user = current_user
@sa = sa
mail(to: user.email, subject: "#{current_user.name} (#{current_user.email}) has invited you to the #{sa} account ")
end
因此,您可以将任何主题/数据放入邮件正文中。
祝你好运!答案 1 :(得分:4)
查看my answer类似的问题,这可能有所帮助。
编辑,因此您似乎需要在资源类中定义公共headers_for方法。
解决方案:在User.rb中放置此方法的某个版本,确保它是公开的。
def headers_for(action)
action_string = action.to_s
case action_string
when "invitation" || "invitation_instructions"
{:from => 'foo@bar.com'}
else
{}
end
end
您必须返回哈希值,因为Devise::Mailer
将尝试合并哈希值。
答案 2 :(得分:3)
看看devise_invitable wiki。
class User < ActiveRecord::Base
#... regular implementation ...
# This method is called interally during the Devise invitation process. We are
# using it to allow for a custom email subject. These options get merged into the
# internal devise_invitable options. Tread Carefully.
#
def headers_for(action)
return {} unless invited_by && action == :invitation_instructions
{ subject: "#{invited_by.full_name} has given you access to their account" }
end
end