Sendgrid-nodejs: 尝试发送事务性电子邮件时发生意外错误

创建于 2017-08-31  ·  23评论  ·  资料来源: sendgrid/sendgrid-nodejs

发行摘要

使用以下有效负载获取“错误请求”错误。

请求

cosnt msg = {
        "to": "[email protected]",
        "from": "[email protected]",
        "subject": "Support Pay Transparency at PayCheck",
        "templateId": "24ae3147-4faa-4380-8613-c5be144f4542",
        "customArgs": {
            "ally_id": "cj6zlh7yd000001qir4r5suuk"
        }
};
sgMail.send(msg)

响应

{
        "message": "Bad Request",
        "code": 400,
        "response": {
            "headers": {
                "server": "nginx",
                "date": "Wed, 30 Aug 2017 22:30:41 GMT",
                "content-type": "application/json",
                "content-length": "365",
                "connection": "close",
                "access-control-allow-origin": "https://sendgrid.api-docs.io",
                "access-control-allow-methods": "POST",
                "access-control-allow-headers": "Authorization, Content-Type, On-behalf-of, x-sg-elas-acl",
                "access-control-max-age": "600",
                "x-no-cors-reason": "https://sendgrid.com/docs/Classroom/Basics/API/cors.html"
            },
            "body": {
                "errors": [
                    {
                        "message": "Unless a valid template_id is provided, the content parameter is required. There must be at least one defined content block. We typically suggest both text/plain and text/html blocks are included, but only one block is required.",
                        "field": "content",
                        "help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content"
                    }
                ]
            }
        }
    }

笔记

在文档中显示templateId但在错误消息中显示template_id 。 我尝试了两者,并收到了相同的错误消息。 我附加了模板屏幕的屏幕截图,以进行完整性检查,以确保已在sendgrid中正确配置了它。

技术细节:

  • sendgrid-nodejs版本:master(最新提交:[commit number])
  • Node.js版本:6.10.3
  • 在AWS Lambda函数中运行

screen shot 2017-08-30 at 6 43 57 pm

help wanted bug

最有用的评论

感谢您的帮助。 我真的失去了理智。 我终于在昨晚想出了解决方法,正要来这里报告更多信息,但似乎大家都已经开始:-)

所有23条评论

@onesien新节点JS邮件程序API不区分大小写,因此可以同时使用templateIdtemplate_id 。 驼峰式案例在JS领域更为常见,因此我们增加了对此的支持,而API本身使用了蛇形案例。

我看一下模板ID发生了什么

@onesien

您可能还需要获得支持。 我认为问题可能与旧的模板编辑器有关。

您还可以尝试制作一个新模板并使用该ID吗?

最诚挚的问候,

埃尔默

@thinkingserious您可以在此检查一下吗? 似乎我们在正确发送带有请求的template_id参数。 我刚刚创建了一个新模板,并且也遇到了这个问题。

@adamreisnz

我的本地测试正在运行,但是我的帐户上只有新模板。

我会仔细检查

不,我使用了新创建的新模板,出现了同样的问题

对我来说,下面的工作(使用版本6.1.1)以下的例子在这里

// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey(process.env.SENDGRID_API_KEY);
sendGridMail.setSubstitutionWrappers('-', '-');

//build object 
var mailOptions = {
    personalizations:[{
      to: '[email protected]',
      substitutions: {'name':'John', 'city':'Denver'}
    }],
    from: '[email protected]',
    reply_to: '[email protected]',
    subject: 'Hello',
    html: 'email text goes here',
    templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
  };

//send 
sendGridMail.send(mailOptions);

嗯,我认为我们有一个很老的bug重新出现,在所有情况下API都需要内容块。 所以这工作:

// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey(process.env.SENDGRID_API_KEY);
sendGridMail.setSubstitutionWrappers('-', '-');

//build object 
var mailOptions = {
    to: '[email protected]',
    from: '[email protected]',
    reply_to: '[email protected]',
    subject: 'Hello',
    templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
    html: ' ',
    substitutions: {
      name: 'Some One',
      city: 'Denver',
    }
  };

//send 
sendGridMail.send(mailOptions);

但不是这个:

// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey(process.env.SENDGRID_API_KEY);
sendGridMail.setSubstitutionWrappers('-', '-');

//build object 
var mailOptions = {
    to: '[email protected]',
    from: '[email protected]',
    reply_to: '[email protected]',
    subject: 'Hello',
    templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
    substitutions: {
      name: 'Some One',
      city: 'Denver',
    }
  };

//send 
sendGridMail.send(mailOptions);

内部检查...

我必须添加内容块来获取要发送的模板。 我收到一个_Bad Request(400)“使用新html编辑器中创建的模板时,“除非提供了有效的template_id,否则content参数是必需的。必须至少有一个已定义的内容块...” _。

sgMail.setApiKey(sgKey);
sgMail.setSubstitutionWrappers('-', '-');
var msg: any = {
  to: email,
  from: '[email protected]',
  templateId: 'xxxxxxx',
  substitutions: {
     verifyUrl: createAccountLink
  }
};
sgMail.send(msg)

如果添加了内容块,则模板电子邮件将以html编辑器中的格式发送。

sgMail.setApiKey(sgKey);
sgMail.setSubstitutionWrappers('-', '-');
var msg: any = {
  to: email,
  from: '[email protected]',
  content: [{"type":"text/html","value":"0"}],
  templateId: 'xxxxxx',
  substitutions: {
     verifyUrl: createAccountLink
  }
};
sgMail.send(msg)

好的,我将其保留为错误。

我的猜测是,此SDK在未设置时会添加一个空的内容对象。 我认为当未设置html和txt时,我们需要跳过这些功能

目前,上述解决方法将满足您的需求。

@thinkingserious我认为你是对的。 我阅读了API文档,并认为该字段是必填字段。 我只错过了一个很好的隐藏评论(样式惊人!):

image

我将创建一个PR来解决这个问题!

呵呵,谢谢亚当!

PR已启动,这应该可以解决问题,并且如果没有提供内容,则不要随请求一起发送内容字段。

感谢您的帮助。 我真的失去了理智。 我终于在昨晚想出了解决方法,正要来这里报告更多信息,但似乎大家都已经开始:-)

抱歉@onesien

该修复程序刚刚被推送到npm(v6.1.2)。

我使用了@thinkingserious的响应中的逐字代码,但仍然

我只是想在我在sengrid中创建的模板中获取最基本的替换,以进行发送。 CloudWatch中的响应(因为它正在Lambda中运行)显示为202,但我收到的电子邮件是: A message was received from this address by our systems that had errors in the smtpapi header, and cannot be processed. The error detected was: The template id must be a valid template id for your account.

伙计们,我在做什么错? 我正在尝试通过sendgrid文档进行筛选,但是一切都感觉很零碎。

您如何发送模板ID? 调用lambda时是否通过环境变量进行?

感谢您的快速回复@cbilliau-在我的情况下,模板字符串没有经过硬编码,因为我只是想在前端和后端之间建立功能支架。 我的逐字记录示例是:

const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(
  "XXXXX"
);
sgMail.setSubstitutionWrappers("%", "%"); // Configure the substitution tag wrappers globally
  const msg = {
    to: formFields.toEmail,
    from: formFields.fromEmail,
    subject: "Hello world",
    text: "test",
    html: "<p>test</p>",
    templateId: "be2cc0da-5b2c-428f-8e45-c140f6cfb6eb",
    substitutions: {
      name1: formFields.fromName,
      name2: formFields.fromName,
      date: formFields.date
    }
  };

  sgMail.send(msg);

@tetreault在上面查看我的答案,我在函数中添加了content: [{"type":"text/html","value":"0"}],并成功运行。 不知道为什么。

刚刚尝试过,不幸的是并没有更改最终结果@cbilliau :(。返回了相同的电子邮件:

A message was received from this address by our systems that had errors in the smtpapi header, and cannot be processed. 
The error detected was: The template id must be a valid template id for your account. You provided be2cc0da-5b2c-428f-8e45-c140f6cfb6eb 

哇,让我鞠躬几秒钟@ cbilliau🤣我只是在匆匆忙忙,没有多加注意。 我意识到我正在为在“市场营销”下而不是在“交易模板”下制作的模板获取唯一的ID。 我没有太多使用sendgrid UI,只是完全搞砸了这个哈哈

全脸掌,固定在我的头上,然后希望它能正常工作。

kk-现在确认其工作正常,尤其是使用content: [{ type: "text/html", value: "0" }]行👍

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