2016-04-24 77 views
0

我一直在实现sendgrid-ruby gem通过SendGrid发送电子邮件。我正在使用专门用于发送消息的模板。我使用TemplateMailer实现了所有工作。如何将unique_args从Ruby on Rails传递到SendGrid :: TemplateMailer API

这是代码:

unique_args = {unique_args: {MyAuditNumber: "9999999"}} 

    # Create a sendgid recipient list 
    recipients = [] 

    recipient = SendGrid::Recipient.new(to_email) 
    merge_vars.each do |mv| 
     Rails.logger.debug(mv) 
     recipient.add_substitution('*|' + mv["name"] + '|*', mv["content"]) 
    end 

    recipients << recipient 

    # Create a sendgrid template 
    template = SendGrid::Template.new(template_id) 

    # Create a client 
    client = SendGrid::Client.new(api_key: Rails.configuration.sendgridkey) 
    mail_defaults = { 
     from: from_email, 
     from_name: from_name, 
     to: to_email, 
     to_name: to_name, 
     bcc: bcc, 
     html: ' ', 
     text: ' ', 
     subject: subject 
    } 

    mailer = SendGrid::TemplateMailer.new(client, template, recipients)   

    # send it 
    lres = mailer.mail(mail_defaults) 

我想做的最后一件事是一个唯一的标识符添加到我发送的每条消息。

我读过两个SendGrid文档以及几个问题和其他物品( how to get response of email sent using sendgrid in rails app to save in database http://thepugautomatic.com/2012/08/sendgrid-metadata-and-rails/ https://sendgrid.com/docs/Integrate/Code_Examples/SMTP_API_Header_Examples/ruby.html

我可以告诉大家,我需要unique_args添加到SMTP API。但是我想不出的是如何将它传递给SendGrid例程。

我已经试过了诸如:

recipient.add_to_smtpapi(unique_args) 

recipient.add_to_smtpapi(unique_args.to_json) 

mail_defaults = { 
    smtpapi: unique_args, 
    from: from_email, 
... 

mail_defaults = { 
    smtpapi: unique_args.to_json, 
    from: from_email, 
    ... 

次这些尝试通常导致这样的错误消息: “{\” 未定义的方法`的add_filter” unique_args \ “:{\” MyAuditNumber \ “:\” 9999999 \ “}}”:字符串

没有人知道如何在使用TemplateMailer时传递unique_args?

回答

1

基于gem documentation,你应该做的是:

header = Smtpapi::Header.new 
header.add_unique_arg("MyAuditNumber", "9999999") 
mail_defaults = { 
    smtpapi: header 
    ... 
相关问题