内存中的 Nodemailer 附件不起作用

Nodemailer attachment from memory not working

我正在使用 multernodemailer 从表单中读取图像。我想将这些图像作为附件发送到电子邮件中。 (用例是用户填写表格报告错误并可以上传错误的屏幕截图)

这是我的代码:

// ...
const multer  = require('multer')
// const upload = multer(); // I believe the below is the same thing but more wordy
const upload = multer({storage: multer.memoryStorage()});

// ...
router.post('/reportissue', upload.array('screenshots[]', 3), reportIssue);

// ...
reportIssue = async function (req, res) {
     try {
          let { type, content } = req.body;

          let replyToEmail = /* default reply_to email address */ 'a1@example.us';
          let replyTo = false;
          let reporter_id = req?.user?.user_id;

          // get the user's email who's making the report
          if(reporter_id) {
               let user = null; // ... SQL to get user info here
               if(user?.email) {
                    replyToEmail = user.email;
                    replyTo = true;
               }

          }

          sendIssueReport({
               reply_to_email: replyToEmail, 
               reply_to: replyTo, 
               issue: content, 
               type: type, 
               reporter_id: reporter_id,
               attachments: req.files,
          });
          return res.status(200).send('issue received');
     } catch (err) {
          // ...
     }
     
// ...
sendIssueReport = async function (params) {
     try {
          const { reply_to_email, reply_to, issue, type, reporter_id, attachments } = params;
          
          let transport = exports.createTransport();
          const email = new Email({
               transport: transport,
               send: true,
               preview: false,
               views: {
                    options: {
                         extension: 'ejs',
                    },
                    root: 'app/email/templates',
               },
          });

          let result = await email.send({
               template: 'report_issue',
               message: {
                    from: process.env.NO_REPLY_EMAIL_ADDRESS,
                    to: /* support mailbox */ 'support@example.us',
                    replyTo: (reply_to ?? false) ? reply_to_email : 'a1@example.us',
               },
               locals: {}, // locals go here
               // ***HERE***
               // Even though I've specified the attachments, they never come through.
               attachments: attachments.map(a => ({
                    filename: a.originalname,
                    content: a.buffer,
                    // contentType: a.mimetype, // this didn't appear to have an effect
                  })),
          });
          console.log(`issue email has been sent!`);
          return null; // no issue
     } catch (err) {
          // ...
     }
}

下面包含 attachments[0] 的输出,以防有帮助:

{
  fieldname: 'screenshots[]',
  originalname: 'audible_color.png',
  encoding: '7bit',
  mimetype: 'image/png',
  buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 02 00 00 00 02 00 08 06 00 00 00 f4 78 d4 fa 00 00 00 04 67 41 4d 41 00 00 b1 8f 0b fc 61 05 00 ... 10905 more bytes>,
  size: 10955
}

我还验证了 req.files 正在按预期一直进入(和退出)映射函数。但是,nodemailer 正在某处删除附件,因为它们不存在于已发送的电子邮件中。

我已经寻找过现有的解决方案,但我可以明确地说:我 不想存储图像。我想要将它保存在内存中,并在电子邮件发送后立即忘记。

我想读入文件(任何扩展名)并将这些相同的文件作为附件写入 nodemailer。

感谢任何帮助。

在令人沮丧的事件中...操作员错误。

附件对象必须放在邮件对象中。

// ...
let result = await email.send({
     template: 'report_issue',
          message: {
               from: process.env.NO_REPLY_EMAIL_ADDRESS,
               to: /* support mailbox */ 'support@example.us',
               replyTo: (reply_to ?? false) ? reply_to_email : 'a1@example.us',
               // ***HERE*** DO: Put attachments in the message object!
               // Good:
               attachments: attachments.map(a => ({
                    filename: a.originalname,
                    content: a.buffer,
                    contentType: a.mimetype,
               })), // YES
          },
          locals: {}, // locals go here
          // DO ***NOT*** put attachments here. Bad:
          attachments: [/* ... */], // NO
     });
// ...