【问题标题】:Error when trying to send transactional email from template using sendGrid尝试使用 sendGrid 从模板发送交易电子邮件时出错
【发布时间】:2016-11-11 12:46:28
【问题描述】:

我正在尝试使用 sendGrid 通过节点发送交易电子邮件。下面是我的代码示例。

        const subject = 'Email subject';
        const templateId = 'templateId';
        const sg = require('sendgrid')(secret);
        const request = sg.emptyRequest({
            method: 'POST',
            path: '/v3/mail/send',
            body: {
                "personalizations": [
                    {
                        "bcc": userEmails,
                        "substitutions": {
                            "-userName-": userDetails.name,
                            "-productPrice-": productDetails.price,
                            "-productUrl-": productDetails.url,
                            "-productPercentageDrop-": productDetails.percentageDrop,
                            "-productName-": productDetails.name,
                            "-productOriginalPrice-": productDetails.origPrice,
                            "-productDroppedPrice-": productDetails.dropPrice,
                            "-productImageUrl-": productDetails.imageUrl
                        },
                        "subject": subject.substring(0, 75)
                    }
                ],
                "from": {
                    "email": "myemail",
                    "name": "myname"
                },
                "content": [
                    {
                        "type": "text/html"
                    }
                ],
                "template_id": templateId
            }
        });

        sg.API(request, function (error, response) {
            if (error) {

                console.log('Error response received');
            }
            console.log(response.body.errors);
        });

但每次我运行代码时都会收到以下错误消息。

400 message: 'Bad Request', field: null, help: null

这在试图找出错误原因时并没有那么有用。

正在发送的正文 JSON:

{
"host":"",
"method":"POST",
"path":"/v3/mail/send",
"headers":{

},
"body":{
    "personalizations":[
        {
            "bcc":[
                {
                    "email":"name1@hotmail.com",
                    "name":"name1"
                },
                {
                    "email":"name2@hotmail.com",
                    "name":"name2"
                }
            ],
            "substitutions":{
                "-productPrice-":189.5,
                "-productUrl-":"http://www.tesco.com/direct/humax-fvp-4000t500-m-smart-freeview-play-hd-digital-tv-recorder-with-wi-fi-500gb/483-1785.prd",
                "-productName-":"Tesco direct: Humax FVP-4000T/500 (M) Smart Freeview Play HD Digital TV Recorder with Wi-Fi - 500GB"
            },
            "subject":"Product Tesco direct: Humax FVP-4000T/500 (M) Smart Freeview Play HD Digita"
        }
    ],
    "from":{
        "email":"email@pricetracker.io",
        "name":"Pricetracker"
    },
    "content":[
        {
            "type":"text/html"
        }
    ],
    "template_id":"XXXXXX"
},
"queryParams":{

},
"test":false,
"port":""

}

【问题讨论】:

  • 能否请您发布正在发送的原始 JSON 请求正文?您的模板是否同时包含 subjectcontent
  • @bwest 添加了 JSON 请求
  • 尝试完全删除内容参数
  • 刚刚尝试过,不幸的是没有任何区别。
  • 我的另一个建议是让最简单的用例工作(一封电子邮件,没有订阅,没有替换),然后添加每个部分,直到你可以找出问题所在。

标签: node.js sendgrid


【解决方案1】:

不知道这是否仍然对您有所帮助,但在使用 Sendgrid 替换时,我遇到了一些类似的问题。我正在使用 sendgrid-php 库,但在内部它发送相同的格式。我发现所有替换值都应该转换为字符串。例如,您对 productPrice 的值应该是这样的:

"-productPrice-": "189.5",

带引号。就我而言,我有整数,当我将它们转换为字符串时,一切正常。

【讨论】:

    【解决方案2】:

    如果你没有将字符串值传递给模板文件,那么 SendGrid 会给你

    {"errors":[{"message":"Bad Request","field":null,"help":null}]}
    

    在发送到模板文件之前将所有非字符串值转换为字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2017-10-30
      • 1970-01-01
      • 2021-09-01
      相关资源
      最近更新 更多