Nodemailer 包含 Table 遍历数组

Nodemailer Include Table Iterating Through An Array

我正在尝试使用 nodemailer 发送带有 HTML table 的电子邮件。 table 需要遍历一个数组。目前它的工作方式是为每次迭代发送一封单独​​的电子邮件,而不是一封带有 table 的电子邮件。我已经尝试将 for 循环移动到 transporter.sendMail() 中,但是,它只会不断返回语法错误。

上面定义了 fteData 数组并且工作正常。

如有任何建议,我们将不胜感激。

JS代码

for(var s = 0; s < fteData.length; s++) {
    var rows = fteData[s];
    let login = rows.login;
    let fte = rows.fte;
    let start = rows.start;
    let end = rows.end;
    let sun = rows.sun;
    let mon = rows.mon;
    let tue = rows.tue;
    let wed = rows.wed;
    let thu = rows.thu;
    let fri = rows.fri;
    let sat = rows.sat;
    let lunch_start = rows.lunch_start;
    let lunch_end = rows.lunch_end;
    let lunch2_start = rows.lunch2_start;
    let lunch2_end = rows.lunch2_end;
    let updated_by = rows.updated_by;
    let timestamp = rows.timestamp;


    var transporter = nodemailer.createTransport(smtpTransport()) ;  
      transporter.sendMail({
      'from': 'from@email.com',
      'to': 'to@email.com',
      'subject': 'Test',
      'html':
        '<table style="border: 1px solid #333;">' +
          '<thead>' +
            '<th> login </th>' +
            '<th> fte </th>' +
            '<th> start </th>' +
            '<th> end </th> ' + 
            '<th> sun </th> ' + 
            '<th> mon </th> ' + 
            '<th> tue </th> ' + 
            '<th> wed </th> ' + 
            '<th> thu </th> ' + 
            '<th> fri </th> ' + 
            '<th> sat </th> ' + 
            '<th> lunch_start </th> ' + 
            '<th> lunch_end </th> ' + 
            '<th> lunch2_start </th> ' + 
            '<th> lunch2_end </th> ' + 
            '<th> updated_by </th> ' + 
            '<th> timestamp </th> ' + 
          '</thead>' +
          '<tr>' +
            '<td>' + login + '</td>' +
            '<td>' + fte + '</td>' +
            '<td>' + start + '</td>' +
            '<td>' + end + '</td>' +
            '<td>' + sun + '</td>' +
            '<td>' + mon + '</td>' +
            '<td>' + tue + '</td>' +
            '<td>' + wed + '</td>' +
            '<td>' + thu + '</td>' +
            '<td>' + fri + '</td>' +
            '<td>' + sat + '</td>' +
            '<td>' + lunch_start + '</td>' +
            '<td>' + lunch_end + '</td>' +
            '<td>' + lunch2_start + '</td>' +
            '<td>' + lunch2_end + '</td>' +
            '<td>' + updated_by + '</td>' +
            '<td>' + timestamp + '</td>' +
          '</tr>' +
        '</table>'

    });
  }

用数据构建一个字符串:

let message = (
  '<table style="border: 1px solid #333;">' +
  '<thead>' +
  '<th> login </th>' +
  '<th> fte </th>'  +
  /*...*/
  '</thead>'
); 

for(const { login, fte, start /*...*/ } of fteData) {
   message += (
     '<tr>' +
      '<td>' + login + '</td>' +
      '<td>' + fte + '</td>' +
      '<td>' + start + '</td>' +
      /*...*/
    '</tr>'
   );
}

message +=  '</table>';

然后您就可以轻松发送整条消息了:

const transporter = nodemailer.createTransport(smtpTransport()) ;  

transporter.sendMail({
  'from': 'from@email.com',
  'to': 'to@email.com',
  'subject': 'Test',
  'html': message
});