gpt4 book ai didi

javascript - 发送没有主题的 Sendgrid 电子邮件

转载 作者:行者123 更新时间:2023-12-03 03:49:44 27 4
gpt4 key购买 nike

是否可以通过 Sendgrid 发送没有主题的电子邮件?我尝试将主题字段留空,但收到一条错误消息:

TypeError: Cannot read property 'map' of undefined

这是我的代码...

        var helper = require('sendgrid').mail;
var fromEmail = new helper.Email("myemail@email.com");
var toEmail = new helper.Email("sendEmail@email.com");

//I set the subject to null

var subject = null;

var content = new helper.Content('text/html', "my message");
var mail = new helper.Mail(fromEmail, subject, toEmail, content);

var sg = require('sendgrid')('-----------------');
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});

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

});

我尝试将主题设置为 null"";,但都返回了错误消息。

有什么想法吗?

最佳答案

您无法通过 API 发送主题为空的电子邮件。请参阅此处的文档:https://sendgrid.com/docs/API_Reference/api_v3.html

如果您向下滚动到主题参数,您会看到它显示:

The global, or “message level”, subject of your email. This may be overridden by personalizations[x].subject. minLength 1

而且它还说这是必需的。如果您查看个性化subject参数,它可以覆盖电子邮件的主要subject,它指向这个关于主题限制的SO答案: What is the email subject length limit?

基本上,该库的编写方式是假设该 map 上存在某个主题,并且会出错。

为了测试它,我做了一个像这样的快速 curl :

curl -X POST \
https://api.sendgrid.com/v3/mail/send \
-H 'authorization: Bearer MYAPIKEY' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{
"personalizations": [
{
"to": [
{
"email": "testemail@test.com"
}
],
}
],
"from": {
"email": "testemail@test.com"
},
"content": [
{
"type": "text/plain",
"value": "Hello, World!"
}
]
}

得到了这样的回复:

{
"errors": [
{
"message": "The subject is required. You can get around this requirement if you use a template with a subject defined or if every personalization has a subject defined.",
"field": "subject",
"help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.subject"
}
]
}

关于javascript - 发送没有主题的 Sendgrid 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45217992/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com