【问题标题】:Sendgrid Substitutions TagsSendgrid 替换标签
【发布时间】:2016-07-26 12:59:43
【问题描述】:

我想在我的应用程序中发送一封通知电子邮件,我正在尝试使用 sendgrid。我的应用程序是用 CoffeeScript 编写的。

enter code here 
from_address = 'noreply@example.co'
subject = 'This Is The Subject'
html_body = '<table style="font-family: verdana, tahoma, sans-serif; color: #000;">
          <tr> <td>
          <h2>Hello,</h2> <p>Hello!!</p>
                          <p>%%name%% %%surname%% send you a message</p>
          </table>'
sendgrid = require('sendgrid')(api_key)
Email = sendgrid.Email
email = new Email(
  to: to_address
  from: from_address
  subject: subject
  text: text_body
  html: html_body)

recipients = [
  'example1@example.com'
  'example2@example.com'
]
i = 0
while i < recipients.length
  email.addTo recipients[i]
  i++

substitutions = {
  "%%name%%": [
    "name1",
    "name2"
  ],
  "%%surname%%": [
     "surname1",
     "surname2"
  ]
}

for tag in substitutions
  email.addSubstitution(tag, substitutions[tag])

email.setFilters({
  "templates": {
    "settings": {
      "enable": "1",
       "template_id": "XXXXXX-XXX-XXXX-XXXX-XXXXXXXXXX"
   }
 }
})


sendgrid.send email, (err, json) ->
  if err
    return console.log(err)
  console.log json
return

当我执行代码时,将我的电子邮件发送到电子邮件地址。但消息是:

你好!!

%%name%% %%surname%% 给你发消息。

替换不起作用。我尝试将 %% 更改为 %,- 和 #。但任何其他似乎都有效。我也尝试使用 setSections。

更新

这是我要发送的 sendgrid 对象。

{ to: [ 'example1@example.com', 'example2@example.com' ],
  from: 'noreply@example.co',
  smtpapi: 
   { version: '1.2.0',
     header: 
      { to: [],
        sub: {},
        unique_args: {},
        category: [Object],
        section: {},
        filters: [Object],
        send_at: '',
        send_each_at: [],
        asm_group_id: {},
        ip_pool: '' } },
  subject: 'This Is The Subject',
  text: 'Hello!\n\nThis is a test message from SendGrid. We have sent this to you because you requested a test message be sent from your account.\n\n This is a link to google.com: http://www.google.com\n This is a link to apple.com: http://www.apple.com\n This is a link to sendgrid.com: http://www.sendgrid.com\n\n Thank you for reading this test message.\n\nLove,\nYour friends at SendGrid',
  html: '<table style="font-family: verdana, tahoma, sans-serif; color: #000;"> <tr> <td> <h2>Hello,</h2> <p>Hello!!</p> <p>%%name%% %%surname%% send you a message</p> </table>',
  bcc: [],
  cc: [],
  replyto: '',
  date: '',
  headers: {},
  toname: undefined,
  fromname: undefined,
  files: [] }

我做错了什么?

提前谢谢你。

【问题讨论】:

  • 你能打印出 sendgrid 对象最终的样子吗?似乎您没有在 x-smtpapi 标头中正确添加替换,但查看完整内容会有所帮助
  • 嗨,@jacobmovingfwd 我在使用 sendgrid.send 方法之前添加了 sendgrid 对象。感谢您的帮助。
  • 没人知道可能是哪个问题?

标签: node.js coffeescript sendgrid substitution transactional-email


【解决方案1】:

我回答我自己的问题是因为我已经找到了问题。

问题与 Sendgrid 无关,而与咖啡脚本有关。在我的一份声明中,我使用了这个:

for tag in substitutions
  email.addSubstitution(tag, substitutions[tag])

我在这里尝试将替换标记添加到电子邮件对象。但是调试我的代码时,我发现循环没有迭代替换变量。所以任何标签都被添加到电子邮件对象中。

我已经更改了之前的代码:

Object.keys(substitutions).forEach((tag)->
  email.addSubstitution(tag, sub[tag])
  return

通过此更改,替换标签被添加到电子邮件对象中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 2010-12-12
    • 2016-11-10
    • 1970-01-01
    • 2012-03-13
    • 2018-05-02
    相关资源
    最近更新 更多