加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何使用ruby on rails在sendgrid中动态设置取

发布时间:2020-12-17 03:05:38 所属栏目:百科 来源:网络整理
导读:我正在使用sendgrid发送邮件.大约有20个邮件模板. 我在sendgrid应用程序“订阅跟踪”的设置中设置了取消订阅模板. 我的要求是针对不同邮件模板的取消订阅链接的不同文本. 目前,只有一个静态取消订阅链接在sendgrid应用程序“订阅跟踪”中设置即将到来. 任何
我正在使用sendgrid发送邮件.大约有20个邮件模板.

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

我的要求是针对不同邮件模板的取消订阅链接的不同文本.

目前,只有一个静态取消订阅链接在sendgrid应用程序“订阅跟踪”中设置即将到来.

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

我按照这个链接To give unsubscribe link in the mail using sendgrid XSMTPAPI header.但我不知道如何在ruby中实现它.

下面是我在user_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

解决方法

您在正确的轨道上,但要使用SendGrid SMTP API,您将为每封电子邮件添加标题,而不是添加到您的设置.在SMTP设置中,您将进一步存储(至少)user_name,密码,地址,SendGrid Docs,详细配置.使用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/plain":"Unsubscribe Here: <% %>"}}}}'

    mail(
      :to => 'george@example.com',:from => email,:subject => 'Example Message'
    )
  end

end

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读