【问题标题】:Nodemailer streaming a file attachmentNodemailer 流式传输文件附件
【发布时间】:2016-04-11 10:45:33
【问题描述】:

我正在尝试发送一个在发出获取请求时触发的文件。我相信文件的内容是 get 请求的 res.body 。查看 nodemailer 文档,我看到可以指定文件的内容。

request.get({ 
      url: 'https://docs.google.com/spreadsheets/export?id='+posting.driveID+'&exportFormat=xlsx', 
      encoding: null,  // Force Request to return the data as Buffer
      headers: {
          Authorization: "Bearer "+access_token
      }
      }, function done (err, res) {
        console.log(res.body);
        var mailOptions={
          from : "<>",
          to : "<>",
          subject : "new download",
          generateTextFromHTML : true,
          html : "<h2>Download "+posting.title+"</h2>",
          attachments: [{
                filename: "test.xlsx",
                content: res.body
          }]
        };
        transporter.sendMail(mailOptions, function(error, resp){
          if(error) {
          }
          else{
            console.log("Message sent: " + resp.message);
          }
          transporter.close();
        });

当 res.body 被记录时,我看到它存在。电子邮件已发送,我在收件箱中收到。但是电子邮件没有附件——它只是一个标准的文本电子邮件。如何让 nodemailer 发送我的附件?

【问题讨论】:

  • 在 res.body 你得到什么样的数据
  • res.body 的日志如下所示:
  • 我尝试使用 nodemailer 文档中的示例:{ filename: 'text1.txt', content: 'hello world!' } 但我也收到一封没有附件的电子邮件。看起来很奇怪。

标签: node.js nodemailer


【解决方案1】:

我想通了。当我使用 0.7.1 时,我正在查看 nodemailer 2.0.0 的文档。语法有点不同。

0.7.1: { 文件名:“test.xlsx”, 内容:res.body }

2.0.0: { 文件名:“test.xlsx”, 内容:res.body }

【讨论】:

    【解决方案2】:

    您可能还想指定内容类型。我能找到的最好的 .xlsx 是这样的:

      {
        fileName: 'myFile.xlsx',
        content: req.body,
        contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 
      }
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 2015-12-23
      相关资源
      最近更新 更多