首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sendgrid替换标记

Sendgrid替换标记
EN

Stack Overflow用户
提问于 2016-04-06 08:39:24
回答 1查看 1.1K关注 0票数 1

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

代码语言:javascript
复制
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

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

你好!!

%%姓名%向您发送消息。

替代不起作用。我尝试将%%改为% %,-和#。但任何一种似乎都是可行的。此外,我还尝试使用setSections。

更新

这是我要发送的sendgrid对象。

代码语言:javascript
复制
{ 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: [] }

我做错了什么?

提前谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2016-04-13 12:00:15

我回答我自己的问题,因为我已经发现了问题。

这个问题不是与Sendgrid有关,而是与coffeeScript有关。在我的一份声明中,我使用了以下内容:

代码语言:javascript
复制
for tag in substitutions
  email.addSubstitution(tag, substitutions[tag])

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

我已经更改了这个代码的前面的代码:

代码语言:javascript
复制
Object.keys(substitutions).forEach((tag)->
  email.addSubstitution(tag, sub[tag])
  return

随着这一变化,替换标签被添加到电子邮件对象中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36445799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档