Ruby on rails 在Rails Action Mailer中,是否可以将默认的from:替换为来自数据库的电子邮件?
我正试图将一封来自用户的电子邮件传递到默认的from:字段,这样它看起来就像是直接来自他们。这是我现在拥有的。有没有办法将动态变量引入默认的from字段Ruby on rails 在Rails Action Mailer中,是否可以将默认的from:替换为来自数据库的电子邮件?,ruby-on-rails,ruby,ruby-on-rails-4,actionmailer,Ruby On Rails,Ruby,Ruby On Rails 4,Actionmailer,我正试图将一封来自用户的电子邮件传递到默认的from:字段,这样它看起来就像是直接来自他们。这是我现在拥有的。有没有办法将动态变量引入默认的from字段 class IntroMailer < ActionMailer::Base default from: "Me@gmail.com" def intro_email(intro) @intro = intro mail(to: @intro.person1_email, subj
class IntroMailer < ActionMailer::Base
default from: "Me@gmail.com"
def intro_email(intro)
@intro = intro
mail(to: @intro.person1_email, subject: 'Testing Intro Email')
end
end
class IntroMailer
默认值只是一个默认地址,因此您可以在发送以下邮件时覆盖它:
mail(from: @some_object.mail, ...)
您可以在Mailer操作的
mail
方法中覆盖此选项:
class IntroMailer < ActionMailer::Base
default from: "Me@gmail.com"
def intro_email(intro, current_user)
mail(to: intro.person1_email, subject: 'Testing Intro Email', from: current_user.email)
end
end
实际上,它在Rails 5.XX中根本不起作用我搜索了这个答案,但没有找到,你能给我指出正确的方向吗@mikejIt不是重复的。我相信,作者正在尝试使用类似lambda表达式的东西作为默认值,就像在作用域中一样。看起来它没有在Rails中实现,但这是可以解决的。这将需要一些黑客,因为我们无法告诉此呼叫背后的用户…对不起,Gary-重新阅读了您的问题的更多细节,可以看到它的不同-已重新打开它。这似乎对我不起作用,我是否将其放在邮件下(收件人:)?我的目标是让当前用户的电子邮件成为发送电子邮件的来源。关于
回复到的好消息。网络应用中的电子邮件很难正确处理,很难找到最新信息,这是不幸的。
class IntroMailer < ActionMailer::Base
default from: "ourteam@oursite.com"
def intro_email(intro, current_user)
mail(to: intro.person1_email, subject: 'Testing Intro Email', reply_to: full_from(current_user))
end
private
def full_from(user)
address = Mail::Address.new user.email
address.display_name = user.full_name
address.format
end
end