【问题标题】:how to send email with .xlsx attachment in sendgrid?如何在 sendgrid 中发送带有 .xlsx 附件的电子邮件?
【发布时间】:2018-06-25 10:12:53
【问题描述】:

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

 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字符串。

请帮忙。

【问题讨论】:

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

标签: javascript node.js sendgrid sendgrid-api-v3


【解决方案1】:

在这个page 上,它准确地描述了你的错误。

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

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

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

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

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,在我的例子中,我使用了 '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'
    };
    

    【讨论】:

      猜你喜欢
      • 2016-10-23
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      相关资源
      最近更新 更多