【问题标题】:Why can't i get sendgrid to replace my variables in my template when using their smtpapi?为什么在使用他们的 smtpapi 时我不能让 sendgrid 替换我模板中的变量?
【发布时间】:2011-12-22 03:25:47
【问题描述】:

我相信我已经关注了 SendGrid 网站上的文档,但到目前为止,当我收到一封通过他们的 API 发送的电子邮件时,它从未替代我在 x-smtpapi 标头中指定的替换。我正在使用 HTTParty 发送这样的请求:

HTTParty.post(Sendgrid::Postman.api_url, { 
    :query => params.merge({ "api_user" => @config[:api_user], "api_key" => @config[:api_key] }), 
    :headers => headers, :format => :json
  })

“参数”如下所示:

{"from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo"}

标题如下所示:

{"X-SMTPAPI"=>"{\"sub\": {\"-first_name-\": [\"Foo\"], \"-email-\": [\"foo@gmail.com\"], \"-login-\": [\"heavysixer\"], \"-last_name-\": [\"Bar\"]}, \"to\": [\"foo@gmail.com\"]}"} 

邮件总是成功送达,但是当它到达收件箱时,应该被替换的值仍然显示为 -first_name- & -last_name-

我错过了什么?我已经搞砸了整整一天?

----------------------------------------------- ------------

更新: 根据下面的建议,我尝试将 x-smtpapi 参数推送到表单帖子中,但我得到了相同的结果。我的帖子的查询字符串现在看起来像这样:

params = {"api_user" => 'foo', "api_key" => 'bar', "from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo", "x-smtpapi"=>{"sub"=>{"-first_name-"=>["foo"], "-email-"=>["foo@gmail.com"], "-login-"=>["foo"], "-last_name-"=>["bar"]}}}

HTTParty.post(Sendgrid::Postman.api_url, :query => params, :format => :json)

【问题讨论】:

    标签: ruby sendgrid


    【解决方案1】:

    他们的documentation 暗示 x-smtpapi 应该是发布的参数之一,而不是 http 标头。

    【讨论】:

    • 如果您查看此文档,它看起来像是在指定标题:docs.sendgrid.com/documentation/api/smtp-api/ruby-example
    • 那是他们的 SMTP api。如果您查看 web api 的实际示例,它是一个参数(示例在 php 中,但很明显发生了什么)
    • 我明白你在说什么。我已将“x-smtpapi”重新定位到查询参数中,但它仍然没有替换这些值。我觉得我错过了一些明显的东西。
    • 事实证明,我需要替换一些生成的 json 才能使其正常工作。但是,您是正确的,它确实作为帖子中的参数而不是标题中的参数。非常感谢你的帮助! {'x-smtpapi' => { 'to' => @params['to'], 'sub' => @params['sub'] }.to_json.gsub(/(["]}])([ ,:])(["[{])/, '\\1\\2 \\3')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2017-02-15
    • 2018-10-18
    相关资源
    最近更新 更多