Kri*_*hoo 4 ruby-on-rails sendgrid
我正在使用sendgrid发送邮件.大约有20个邮件模板.
我在sendgrid应用程序"订阅跟踪"的设置中设置了取消订阅模板.
我的要求是针对不同邮件模板的取消订阅链接的不同文本.
目前,unsubscribe linksendgrid应用程序"订阅跟踪"中设置的静态只有一个.
任何人都可以帮助我如何动态设置我user_mailer班级中的取消订阅链接.
我按照此链接使用sendgrid XSMTPAPI标头在邮件中提供取消订阅链接.但我不知道如何在ruby中实现它.
下面是我在我的尝试中的代码user_mailer class.
def abuse_notification(post,current_user,eventid)
headers['X-SMTPAPI'] = '{"filters":{"subscriptiontrack":{"settings":{"enable":1,"text/html":"Unsubscribe <%Here%>","text/plain":"Unsubscribe Here: <% %>"}}}}'.to_json()
UserNotifier.smtp_settings.merge!({:user_name => "info@xxxx.example.com"})
@recipients = "test@xxx.example.com"
@from = "xxxx"
@subject = "Report Abuse Notification"
@sent_on = Time.now
@body[:user] = current_user
@body[:event] = post
end
Run Code Online (Sandbox Code Playgroud)
您在正确的轨道上,但要使用SendGrid SMTP API,您将为每封电子邮件添加标题,而不是添加到您的设置.在您的SMTP设置,你会存储您的(至少)user_name,password,address,在SendGrid文档进一步,详细的配置.随着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
}
Run Code Online (Sandbox Code Playgroud)
配置ActionMailer后,您需要设置UserNotifier类以查找类似于以下内容的类.每个方法都会设置X-SMTPAPI标题:
class UserNotifier < ActionMailer::Base
default :from => "bob@example.com"
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 => 'george@example.com',
:from => email,
:subject => 'Example Message'
)
end
end
Run Code Online (Sandbox Code Playgroud)
请注意,X-SMTPAPI标头是JSON,如果您希望将Ruby对象转换为JSON,则需要使用JSONgem.