Javascript 如何在sendgrid中发送带有.xlsx附件的电子邮件?

Javascript 如何在sendgrid中发送带有.xlsx附件的电子邮件?,javascript,node.js,sendgrid,sendgrid-api-v3,Javascript,Node.js,Sendgrid,Sendgrid Api V3,下面是用于发送电子邮件的消息对象 message = { to: toEmail, from: emailInfo.emailFromAddress, subject: emailInfo.emailSubjectTemplate, attachments: [ { filename: fileName, content: base64str,

下面是用于发送电子邮件的消息对象

 message = {
        to: toEmail,
        from: emailInfo.emailFromAddress,
        subject: emailInfo.emailSubjectTemplate,
        attachments: [
          {
            filename: fileName,
            content: base64str,
            contentId: fileName,
            disposition: "attachment"
          }
        ],
        html: emailMessageBodyTemplate
      };
以下代码将内容编码为base64字符串

const base64_encode = file => {
  var bitmap = fs.readFileSync(file);
  return new Buffer(bitmap).toString("base64");
};
我不知道我哪里出错了,但我得到的错误如下

消息:“内容值必须是长度至少为一个字符的字符串。”

但是当我调试它是base64字符串时,它的内容不是空的

请帮助。

在这方面,它准确地描述了您的错误

我相信这个错误的内容是指你的信息或错误描述的文本

您不能发送没有内容的电子邮件

根据文档,您缺少必需的参数内容

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
          };
希望这有帮助。

在这方面,它准确地描述了您的错误

我相信这个错误的内容是指你的信息或错误描述的文本

您不能发送没有内容的电子邮件

根据文档,您缺少必需的参数内容

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
          };

希望这能有所帮助。

我也遇到了同样的问题,在我的例子中,我使用了“xlsx”npm lib来实现解决方案,如下所示:

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', compression: true });

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

我遇到了同样的问题,在我的例子中,我使用“xlsx”npm lib来实现解决方案,如下所示:

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', compression: true });

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

我的回答解决了你的问题吗?是的,我只是忘了投票谢谢你的帮助!!!快乐编码;)我的回答解决了你的问题吗?是的,我只是忘了投票谢谢你的帮助!!!快乐编码;)