您的方向是正确的,但是要使用 SendGrid SMTP API,您需要将标头添加到每封电子邮件中,而不是添加到您的设置中。在您的 SMTP 设置中,您将(至少)存储您的user_name
, password
, address
, the 发送网格文档,进一步详细配置。和ActionMailer
您可以按如下方式配置它:
ActionMailer::Base.smtp_settings = {
:user_name => 'sendgridusername',
:password => 'sendgridpassword',
:domain => 'yourdomain.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
配置 ActionMailer 后,您需要设置您的UserNotifier
类看起来类似于以下内容。每个单独的方法都会设置X-SMTPAPI
header:
class UserNotifier < ActionMailer::Base
default :from => "[email protected]"
def send_message(name, email, message)
@name = name
@email = email
@message = message
headers['X-SMTPAPI'] = '{"filters":{"subscriptiontrack":{"settings":{"enable":1,"text/html":"Unsubscribe <%Here%>","text/plain":"Unsubscribe Here: <% %>"}}}}'
mail(
:to => '[email protected]',
:from => email,
:subject => 'Example Message'
)
end
end
请注意,X-SMTPAPI
标头采用 JSON 格式,如果您希望将 Ruby 对象转换为 JSON,则需要使用JSON
gem.