Sendgrid-nodejs: 如何使用node.js在模板中进行替换

创建于 2016-11-30  ·  13评论  ·  资料来源: sendgrid/sendgrid-nodejs

发行摘要

我正在使用API​​ 3将sendgrid与我的node.js应用程序集成。 我可以使用基本的电子邮件,但是我需要用数据替换模板中的某些内容。 下面是我用来发送电子邮件的代码,电子邮件熄灭了,但是替代项不起作用。

var sg = require('sendgrid')('API-KEY');

    var request = sg.emptyRequest();
    request.body = {
        "from": {
            "email": "[email protected]",
            "name": "Admin"
        },
        "personalizations" : [
            {
                "to": [
                    {
                        "email": recepientEmail,
                        "name": "User"
                    }
                ]
            }
        ],
        "subject": "This is subject",
        "template_id": "template-id-here",
        "substitution": {
            "-sub1-": ["This is the new substitured text"],
            "-sub2-": ["Substitured text"]
        }
    };
    request.method = 'POST';
    request.path = '/v3/mail/send';

    sg.API(request, function(error, response) {
        console.log(response.statusCode)
        console.log(response.body)
        console.log(response.headers)
    });

技术细节:

  • sendgrid-nodejs版本:master(最新提交:[commit number])
  • Node.js版本:4.1.2
question

最有用的评论

只是@thinkingserious提到的上述url的更新略有更改: https :

所有13条评论

@ neeraj87

我希望这些示例对您有所帮助: https :

如果您遇到任何问题,请告诉我。 谢谢!

@ neeraj87

简而言之,您的JSON结构不正确。

替换必须在个性化数组内部。 这样,您可以为每个人定义不同的替代人选。

另外,替换数组的name(key)必须为替换

"substitutions": { "-sub1-": "This is the new substitured text", "-sub2-": "Substitured text" }

@thinkingserious非常感谢您提供的文档链接,它确实有效。 我还注意到我缺少替代中的复数“ s”。

@alperenozlu感谢您的回答,修复了JSON结构和键值对。

@thinkingserious一个快速的问题,我正在尝试使用变量替换标记,但它一直给我:错误:[{消息:“错误请求”,字段:空,帮助:空}]

var request = sg.emptyRequest();
    request.body = {
        "from": {
            "email": "[email protected]",
            "name": "Admin"
        },
        "personalizations" : [
            {
                "to": [
                    {
                        "email": recepientEmail,
                        "name": userNameVariable
                    }
                ],
                "substitution": {
                    "-sub1-": variableB,
                    "-sub2-": variableA
                }
            }
        ],
        "subject": "This is subject",
        "template_id": "template-id-here"
    };
    request.method = 'POST';
    request.path = '/v3/mail/send';

    sg.API(request, function(error, response) {
        console.log(response.statusCode)
        console.log(response.body)
        console.log(response.headers)
    });

这是我得到的错误

400
{错误:[{消息:'Bad Request',字段:null,帮助:null}]}
{服务器:“ nginx”,
date:'Thu,01 Dec 2016 07:08:51 GMT',
'content-type':'application / json',
'content-length':'63',
连接:“保持活动”}

@ neeraj87

请检查变量的类型,其类型必须为字符串。 另外,请将替换键更改为替换

@alperenozlu您说得对,其中一个替代是整数。 将其转换为字符串,它可以工作。

谢谢您的帮助。

大家好!
如何用值数组而不是字符串替换?

@Deniscapp您可以读取数组元素,将它们连接起来以形成字符串。

@ neeraj87那就是我所做的! 但是我只是想知道他们是否以某种方式添加了此功能。
感谢您的回答! :)

@Deniscapp糟糕,不知道。 我认为它是较旧的版本。 也许来自SendGrid的“官方”人可以回答这个问题。

@Deniscapp,@ neeraj87

从API文档:

遵循模式“ substitution_tag”:“要替换的值”的键/值对的集合。 假定所有都是字符串。 除了主题和回复参数之外,这些替代还将应用于电子邮件正文的文本和html内容。

使库允许使用数组,然后将它们连接起来以在幕后形成字符串,这可能会很有用。

只是@thinkingserious提到的上述url的更新略有更改: https :

谢谢@vaskort!

我已经更新了原始评论中的链接。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

amlcodes picture amlcodes  ·  4评论

murphman300 picture murphman300  ·  4评论

umarhussain15 picture umarhussain15  ·  3评论

thinkingserious picture thinkingserious  ·  4评论

agostonbonomi picture agostonbonomi  ·  3评论