我正在尝试将一封来自用户的电子邮件传递给默认的发件人:字段,使其看起来像是直接来自他们。这是我现在所拥有的。有什么方法可以将动态变量引入默认值 from 字段吗?
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
最佳答案
您可以在 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
但是一个警告。像谷歌这样的电子邮件客户端在检测垃圾邮件方面非常聪明。如果他们发现特定的 SMTP 服务器正在发送具有许多不同“发件人”属性的电子邮件,您的垃圾邮件评级将会上升,并且您的电子邮件将被垃圾邮件过滤器过滤掉。要解决此问题,请选择适合电子邮件类型的一两个默认 from
电子邮件(例如 support@mywebsite.com 和 jobs@mywebsite.com),然后添加动态 reply_to
属性代替。
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
关于ruby-on-rails - 是否可以从 : with an email from database in Rails Action Mailer? 替换默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27138059/