1

是否可以通过 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"";但都返回了错误消息。

有任何想法吗?

4

1 回答 1

2

您不能通过 API 发送带有空subject. 在此处查看文档:https ://sendgrid.com/docs/API_Reference/api_v3.html

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

电子邮件的全局或“消息级别”主题。这可能会被个性化[x].subject 覆盖。最小长度 1

它还说它是必需的。如果您查看可以覆盖电子邮件 main的subject参数,它指向关于主题限制的 SO 答案:电子邮件主题长度限制是多少?personalizationsubject

因此,基本上该库的编写方式是假设该地图上存在主题并且会出错。

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

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"
        }
    ]
}
于 2017-07-25T08:16:47.550 回答