如何使用 ruby​​ on rails 在 sendgrid 中动态设置取消订阅链接 [英] How to dynamically set unsubscribe link in sendgrid using ruby on rails

查看:58
本文介绍了如何使用 ruby​​ on rails 在 sendgrid 中动态设置取消订阅链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 sendgrid 发送邮件.大约有 20 个邮件模板.

I am using sendgrid for sending mails. There are around 20 mail templates.

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

I have set unsubscribe template in the settings of sendgrid app "Subscription Tracking".

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

My requirement is different texts for unsubscribe link for different mail templates.

目前,只有一个在 sendgrid 应用程序订阅跟踪"中设置的静态取消订阅链接即将推出.

Currently, only one static unsubscribe link as set in the sendgrid app "Subscription Tracking" is coming.

谁能帮助我如何在我的 user_mailer 类中动态设置取消订阅链接.

Can any one help me how to dynamically set the unsubscribe link in my user_mailer class.

我点击了这个链接 取消订阅使用 sendgrid XSMTPAPI 标头在邮件中链接.但是我不知道如何在 ruby​​ 中实现它.

I followed this link To give unsubscribe link in the mail using sendgrid XSMTPAPI header. But I do not know how to implement it in ruby.

以下是我在 user_mailer 类 中尝试过的代码.

Below is the code what I have tried in my user_mailer class yet.

    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

推荐答案

您走在正确的轨道上,但是要使用 SendGrid SMTP API,您需要将标头添加到每封电子邮件中,而不是添加到您的设置中.在您的 SMTP 设置中,您将(至少)存储您的 user_namepasswordaddressSendGrid Docs,进一步详细配置.使用 ActionMailer,您可以将其配置如下:

You're on the the right track, however to use the SendGrid SMTP API, you'll add the header to each email, rather than to your settings. In your SMTP settings you'll store your (at a minimum) user_name, password, address, the SendGrid Docs, detail configuration further. With ActionMailer you would configure it as follows:

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 标头:

Once you've configured ActionMailer you'll need to set up your UserNotifier class to look something similar to the following. Each individual method will set the X-SMTPAPI header:

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

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

Note that the X-SMTPAPI header is in JSON, if you wish to convert a Ruby object into JSON, you'll need to use the JSON gem.

这篇关于如何使用 ruby​​ on rails 在 sendgrid 中动态设置取消订阅链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆