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

Send Sendgrid Email Without Subject

是否可以通过 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 发送带有空 subject 的电子邮件。请在此处查看文档: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

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

所以基本上这个库是以这样一种方式编写的,它假设一个主题存在于该地图上并且它会出错。

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

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"
        }
    ]
}