最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to send email with .xlsx attachment in sendgrid? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jun 25, 2018 at 13:23 Krunal Limbad 1,5731 gold badge13 silver badges24 bronze badges asked Jun 25, 2018 at 10:12 roshrosh 973 silver badges13 bronze badges 2
  • did my answer solved your problem ? – Krunal Limbad Commented Jun 26, 2018 at 8:18
  • yeah, I just forgot to upvote thanks for the help!!! happy coding;) – rosh Commented Jun 27, 2018 at 7:19
Add a ment  | 

3 Answers 3

Reset to default 3

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.

I've encountered the same issue and in my case I used 'xlsx' npm lib, to implement the solution as follow:

const workbook = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet(data);
XLSX.utils.book_append_sheet(workbook, ws, 'Accounts');

// write the file in base64 format
const report = XLSX.write(workbook, { type: 'base64', pression: true });

const attachment = {
    content: report,
    filename: `MyReport.xlsx`,
    type: 'text/html',
    disposition: 'attachment'
};

I'm also using XLSX but found using base64 format caused issues with the file extension.

I found setting the output data encoding to buffer solved the issue for me.

const content = write(workbook, { type: 'buffer', pression: true });
发布评论

评论列表(0)

  1. 暂无评论