我有很多邮件(*Mailer
(。一切都在SMTP上运行。
我了解如何为特定的*Mailer
方法设置标题。但是如何全局设置标题?也就是说,我需要我的应用程序发送的所有字母都有我的标头。这样就不会与单个邮件设置冲突。
我试图在文档(和谷歌(中找到,但没有找到任何东西。
你可以做这样的事情。
class ApplicationMailer < ActionMailer::Base
default from: "email@company.com", "HEADER_KEY" => "VALUE"
end
所有的邮件程序都应该继承自 ApplicationMailer,而 ApplicationMailer 本身也继承自 ActionMailer::Base。
在ApplicationMailer中,您可以定义默认的smtp标头,默认布局等。 这是我的application_mailer.rb,给你一些可以包含的想法:
#application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "Site Admin<#{NO_REPLY_EMAIL}>"
layout 'mailer'
def mail
super(options)
end
private
def options
{:'List-Unsubscribe-Post' => :'List-Unsubscribe=One-Click',
:'List-Unsubscribe' => unsubscribe_url,
:subject => t('.subject', org_name: ORGANIZATION_NAME, app_name: APPLICATION_NAME),
:to => "#{@recipient.email}",
:date => Time.now }
end
def unsubscribe_url
params = { :locale => I18n.locale,
:user_id => @recipient.id,
:unsubscribe_code => @recipient.refresh_unsubscribe_code,
:protocol => :https }
@unsubscribe_url = admin_unsubscribe_url( params )
end
对于 Rails 5:
class ApplicationMailer < ActionMailer::Base
layout 'mailer'
def mail(args)
headers('X-MyCorp-customer' => @customer&.name)
headers('X-MyCorp-env' => Rails.env)
headers('X-MyCorp-app' => 'XRay Insights')
super(args)
end
end