node.js - Sendgrid 替换标签

标签 node.js coffeescript sendgrid substitution transactional-email

我想在我的应用程序中发送通知电子邮件,并且我正在尝试使用 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 无关,而是与 CoffeeScript 有关。在我的一份声明中,我使用了这个:

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

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

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

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

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

关于node.js - Sendgrid 替换标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36445799/

相关文章:

cpanel - 不同域的不同智能主机具有不同的凭据

laravel - 无法使用带有 Laravel 5.4 的 sendgrid 发送电子邮件并发现错误

node.js - 如何在 log4js smtp 附加程序中使用非 SMTP 传输?

JavaScript 相当于 PHP 的 Compact() 方法?

javascript - 以编程方式创建 aws API key 并将其添加到单一功能的使用计划中?

javascript - Node.js readline-sync 卡住我的服务器(Windows)

node.js - socketTimeoutMS 在非常简单的环境中不起作用

javascript - 函数的括号包裹 - (function() { }) () 和 `.call()`

javascript - AMD 和 CoffeeScript

coffeescript - 检查数组中是否存在具有特定属性值的对象