1

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

enter code here 
from_address = '[email protected]' 
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 = [ 
    '[email protected]' 
    '[email protected]' 
] 
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: [ '[email protected]', '[email protected]' ], 
    from: '[email protected]', 
    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: [] } 

我在做什么错了?

预先感谢您。

+0

你能打印出什么sendgrid对象最终看起来像?好像你没有正确地在'x-smtpapi'头中添加替换,但看到完整的东西会有帮助 – jacobmovingfwd

+0

嗨,@jacobmovingfwd在使用sendgrid.send方法之前,我添加了sendgrid对象。感谢您的帮助。 – jasc

+0

没有人知道哪个可能是问题? – jasc

回答

0

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

该问题与Sendgrid无关,但与coffeeScript相关。在我的一个声明中,我使用这个:

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

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

我已经改变了以前的代码为这一个:

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

而且随着这一变化的替代标签添加到电子邮件对象。