【问题标题】:How to dynamically set unsubscribe link in sendgrid using ruby on rails如何使用 ruby​​ on rails 在 sendgrid 中动态设置取消订阅链接
【发布时间】:2013-08-06 12:05:15
【问题描述】:

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

我在 sendgrid 应用的设置中设置了退订模板“订阅跟踪”

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

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

谁能帮助我如何在我的user_mailer 类中动态设置退订链接。

我点击了这个链接To give unsubscribe link in the mail using sendgrid XSMTPAPI header。但我不知道如何在 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

【问题讨论】:

    标签: ruby-on-rails sendgrid


    【解决方案1】:

    您在正确的轨道上,但是要使用 SendGrid SMTP API,您需要将标头添加到每封电子邮件,而不是添加到您的设置中。在您的 SMTP 设置中,您将存储(至少)user_namepasswordaddressSendGrid 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/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。

    【讨论】:

    • 我按照你的建议试过了。但是,取消订阅链接是按照 订阅跟踪 中的设置出现的,而不是像我在代码中发送的那样。我应该删除sendgrid 中的订阅跟踪设置吗
    • 不,不要删除 SendGrid 网站上的订阅跟踪应用程序。您的电子邮件的所有其他部分是否正在发送(除了订阅跟踪,它看起来应该是正常的)?
    • 是的,除了标题之外,我的邮件程序类中的其他东西都可以正常工作。为了您的清楚,我正在编辑我的问题以添加我的邮件类方法的详细信息。
    • headers['X-SMTPAPI'] = '…'.toJSON()行中删除.toJSON()
    猜你喜欢
    • 2016-03-10
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多