Ruby on rails 如何使用ruby on rails在sendgrid中动态设置取消订阅链接

Ruby on rails 如何使用ruby on rails在sendgrid中动态设置取消订阅链接,ruby-on-rails,sendgrid,Ruby On Rails,Sendgrid,我正在使用sendgrid发送邮件。大约有20个邮件模板 我已在sendgrid应用程序的设置中设置了“取消订阅模板”“订阅跟踪” 我的要求是不同的文本为不同的邮件模板退订链接 目前,只有sendgrid应用程序“订阅跟踪”中设置的一个静态取消订阅链接 任何人都可以帮助我如何在我的user\u mailer类中动态设置取消订阅链接 我跟踪了这个链接。但我不知道如何在ruby中实现它 下面是我在user\u mailer类中尝试过的代码 def abuse_notification(pos

我正在使用sendgrid发送邮件。大约有20个邮件模板

我已在sendgrid应用程序的设置中设置了“取消订阅模板”“订阅跟踪”

我的要求是不同的文本为不同的邮件模板退订链接

目前,只有sendgrid应用程序“订阅跟踪”中设置的一个静态
取消订阅链接

任何人都可以帮助我如何在我的
user\u mailer
类中动态设置取消订阅链接

我跟踪了这个链接。但我不知道如何在ruby中实现它

下面是我在
user\u mailer类中尝试过的代码

    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
def滥用通知(post、当前用户、eventid)
标题['X-SMTPAPI']='{“过滤器”:{“subscriptiontrack”:{“设置”:{“启用”:1,“文本/html”:“取消订阅”,“文本/plain”:“在此处取消订阅:”}}}。到_json()
UserNotifier.smtp_settings.merge!({:user_name=>)info@xxxx.example.com"})
@收件人=”test@xxx.example.com"
@from=“xxxx”
@主题=“报告滥用通知”
@发送时间=现在
@body[:user]=当前用户
@正文[:事件]=帖子
结束

您的思路是正确的,但是要使用SendGrid SMTP API,您需要将标题添加到每封电子邮件中,而不是添加到您的设置中。在SMTP设置中,您将(至少)存储您的
用户名
密码
地址
、详细配置。使用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
标题:

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
类UserNotifier”bob@example.com" def发送消息(姓名、电子邮件、消息) @name=name @电子邮件=电子邮件 @消息=消息 标题['X-SMTPAPI']='{“过滤器”:{“subscriptiontrack”:{“设置”:{“启用”:1,“文本/html”:“取消订阅”,“文本/普通”:“此处取消订阅:}}” 邮寄( :to=>'george@example.com', :from=>电子邮件, :subject=>“示例消息” ) 结束 结束

请注意,
X-SMTPAPI
头是JSON格式的,如果您希望将Ruby对象转换为JSON,则需要使用
JSON
gem。

我按照您的建议尝试了。但是退订链接是根据订阅跟踪中的设置来的,而不是我发送代码的方式。如果我要删除sendgrid中的订阅跟踪设置,请不要删除sendgrid站点上的订阅跟踪应用程序。您的电子邮件发送的所有其他部分(除了订阅跟踪,它看起来应该是这样的)都正常吗?是的,在我的mailer类中,除了标题之外,其他部分都正常工作。为了清楚起见,我正在编辑我的问题以添加我的mailer类方法的详细信息。从
标题['X-SMTPAPI']='…'.toJSON()
行中删除
.toJSON()