javascriptnode.jssendgridsendgrid-api-v3

how to send email with .xlsx attachment in sendgrid?


Below is the message object used to send emails.

 message = {
        to: toEmail,
        from: emailInfo.emailFromAddress,
        subject: emailInfo.emailSubjectTemplate,
        attachments: [
          {
            filename: fileName,
            content: base64str,
            contentId: fileName,
            disposition: "attachment"
          }
        ],
        html: emailMessageBodyTemplate
      };

The content is encoded into a base64 string by the following below code.

const base64_encode = file => {
  var bitmap = fs.readFileSync(file);
  return new Buffer(bitmap).toString("base64");
};

I don't know where I m going wrong but I'm getting the error as follows.

message:"The content value must be a string at least one character in length."

but the content is not empty when I debug it is a base64 string.

Please help.


Solution

  • On this page it describes exactly your error.

    I believe in this error content means your message or a text as error describes

    You may not send an email with no content.

    And as per the API docs,you are missing a required parameter content.

    message = {
                to: toEmail,
                from: emailInfo.emailFromAddress,
                subject: emailInfo.emailSubjectTemplate,
                content:[
                  {
                     type : 'string',
                     value : 'message'
                  }
                ],
                attachments: [
                  {
                    filename: fileName,
                    content: base64str,
                    contentId: fileName,
                    disposition: "attachment"
                  }
                ],
                html: emailMessageBodyTemplate
              };
    

    Hope this helps.