sendgrid/sendgrid-nodejs

Simple mail send is not working with node lib

Closed this issue · 0 comments

Issue Summary

I'm using simple example to send email, as mentioned in https://www.npmjs.com/package/@sendgrid/mail
I'm getting weird response asking for template_id or content.

Code Snippet

const sendgrid = require('@sendgrid/mail')
sendgrid.setApiKey(process.env.SENDGRID_API_KEY)

const sendEmail = (options) => {
  return sendgrid.send({
    from: options.from,
    to: options.to,
    subject: options.subject,
    text: options.body,
    html: options.body,
  }).then((response) => {
    console.log(response[0].statusCode)
    console.log(response[0].headers)
  }).catch((error) => {
    console.error(JSON.stringify(error))
  })
}

Exception/Log

{
   "message":"Bad Request",
   "code":400,
   "response":{
      "headers":{
         "server":"nginx",
         "date":"Sat, 05 Feb 2022 06:50: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",
         "strict-transport-security":"max-age=600; includeSubDomains"
      },
      "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"
            }
         ]
      }
   }
}

Technical details:

  • sendgrid-nodejs version: "@sendgrid/mail": "^7.6.0",
  • node version: 17.x