简体   繁体   English

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

[英]Send Sendgrid Email Without Subject

Is it possible to send an email through Sendgrid without a subject? 是否可以通过Sendgrid发送没有主题的电子邮件? I tried leaving the subject field blank, but I get an error message that says 我尝试将主题字段留空,但收到一条错误消息,内容为

TypeError: Cannot read property 'map' of undefined

Here is my code... 这是我的代码...

        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) {

        });

I tried setting the subject to null and ""; 我尝试将主题设置为null""; but both returned error messages. 但均返回错误消息。

Any Ideas? 有任何想法吗?

You can't send an email via the API with an empty subject . 您不能通过API发送带有空白subject的电子邮件。 See the Docs here: https://sendgrid.com/docs/API_Reference/api_v3.html 在此处查看文档: https : //sendgrid.com/docs/API_Reference/api_v3.html

If you scroll down to the subject parameter, you'll see that it says: 如果向下滚动到subject参数,您会看到它说:

The global, or “message level”, subject of your email. 电子邮件的全局或“邮件级别”主题。 This may be overridden by personalizations[x].subject. 这可能会被个性化设置[x] .subject覆盖。 minLength 1 minLength 1

And it also says that it's required. 它还说这是必需的。 If you look at the subject param of a personalization which can overwrite the email's main subject , it points to this SO answer about the subject limits: What is the email subject length limit? 如果您查看可以覆盖电子邮件主要subjectpersonalization subject参数,它将指向关于主题限制的SO答案: 什么是电子邮件主题长度限制?

So basically the library is written in such a way that it assumes a subject is present on that map and it will error. 因此,基本上,该库的编写方式是假定该地图上存在一个主题,并且会出错。

To test it I did a quick curl like this: 为了测试它,我做了如下的快速卷曲:

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

And got this response: 并得到此响应:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM