简体   繁体   English

使用Node.js的SendGrid模板中的变量替换不起作用

[英]Variable substitution in SendGrid templates with Nodejs does not work

Following the USE CASE on SendGrids github does manage to send me the e-mail with the correct template, but the substitutions does apparently not work, and is left blank in the resulting e-mail. 在SendGrids上使用USE案例之后,github确实设法向我发送了具有正确模板的电子邮件,但是替换显然不起作用,并且在生成的电子邮件中留为空白。 Server side: 服务器端:

const sgmailer = require("@sendgrid/mail");
sgmailer.setApiKey(process.env.SENDGRID_API_KEY);
sgmailer.setSubstitutionWrappers('{{', '}}');

const msg = {
    to: '...',
    from: 'sender@example.org',
    subject: 'Hello world',
    text: 'Hello plain world!',
    html: '<p>Hello HTML world!</p>',
    templateId: '...',
    substitutions: {
        name: 'Some One',
        city: 'Denver',
    },
};
sgmailer.send(msg)

HTML in template: 模板中的HTML:

 <html> <head> <title></title> </head> <body> Hello {{name}}, <br /><br/> I'm glad you are trying out the template feature! <br /><br/> <%body%> <br /><br/> I hope you are having a great day in {{city}} :) <br /><br/> </body> </html> 

Resulting email in my inbox: 结果在我的收件箱中的电子邮件:

Hello , 你好 ,

I'm glad you are trying out the template feature! 很高兴您正在尝试使用模板功能!

I hope you are having a great day in :) 希望您在:)度过愉快的一天

Here the variables are clearly missing. 这里显然没有变量。 How do I substitute variables correctly? 如何正确替换变量?

Since what I was using was dynamic templates from SendGrid, I cannot use the "substitutions" tag, but must instead use the "dynamic_template_data" tag, see this issue . 由于我使用的是来自SendGrid的动态模板,因此无法使用“ substitutions”标签,而必须使用“ dynamic_template_data”标签,请参见此问题 When changing the msg-object to 将msg-object更改为

const msg = {
    to: '...',
    from: 'sender@example.org',
    subject: 'Hello world',
    text: 'Hello plain world!',
    html: '<p>Hello HTML world!</p>',
    templateId: '...',
    dynamic_template_data: {
        name: 'Some One',
        city: 'Denver',
    },
};

it works. 有用。 This is not documented in the SendGrid-documentation as far as I can see. 据我所知,这未在SendGrid文档中进行记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM