带有二维码图像的 Nodemailer 正文

Nodemailer body with qrcode image

我正在用 express 创建服务器,想发送邮件正文中带有 QRcode 的邮件

var express = require('express');
var app = express();
var nodeMailer = require('nodemailer');
var sql = require("mssql");
var bodyParser = require('body-parser');
var QRCode = require('qrcode')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 

app.post('/send-email', function (req, res) {
        QRCode.toDataURL('data invoice untuk di kirim melalui email', function (err, url) {
            let data = url.replace(/.*,/,'')
            var img = new Buffer(data,'base64')

        })
          let transporter = nodeMailer.createTransport({
              host: 'test',
              port: 587,
              secure: false,
              auth: {
                  user: 'tes',
                  pass: 'password'
              }
          });
          let mailOptions = {
              from: 'test', // sender address
              to: 'test', // list of receivers
              subject: 'Test Email Node JS', // Subject line
              text: 'Halo ini dari node js', // plain text body
              html: 'Halo ini barcodenya </br>' + img // html body
          };

          transporter.sendMail(mailOptions, (error, info) => {
              if (error) {
                  return console.log(error);
              }
              //console.log('Message %s sent: %s', info.messageId, info.response);
                  res.render('index');
              });
      });

我收到一条错误消息,指出 img 未定义。

变量img是二维码,对于字符串,它将从SQL查询中获取字符串。

有这方面的来源吗?

  1. QRCode.toDataURL 是一个异步函数,但您试图同步使用。使用 Promises 和 Async/Await 代替回调 function.
  2. 您将图像作为缓冲区发送,但它需要在 base64image tag

这里是代码片段的变化,

...

app.post('/send-email', async function (req, res) {

    let img = await QRCode.toDataURL('data invoice untuk di kirim melalui email');

    let transporter = nodeMailer.createTransport({
        host: 'test',
        port: 587,
        secure: false,
        auth: {
            user: 'tes',
            pass: 'password'
        }
    });
    let mailOptions = {
        from: 'test', // sender address
        to: 'test', // list of receivers
        subject: 'Test Email Node JS', // Subject line
        text: 'Halo ini dari node js', // plain text body
        html: 'Halo ini barcodenya </br> <img src="' + img + '">' // html body
    };

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }
        //console.log('Message %s sent: %s', info.messageId, info.response);
        res.render('index');
    });
});

...

mailOptions 中,您需要在 true

上添加 attachDataUrls
let mailOptions = {
    from: 'test', // sender address
    to: 'test', // list of receivers
    subject: 'Test Email Node JS', // Subject line
    text: 'Halo ini dari node js', // plain text body
    attachDataUrls: true,//to accept base64 content in messsage
    html: 'Halo ini barcodenya </br> <img src="' + img + '">' // html body
};