带有qrcode图像的NodeMailer主体

zsohkypk  于 2023-05-17  发布在  Node.js
关注(0)|答案(3)|浏览(137)

我用express创建了服务器,想用体内的QR码发送邮件

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是qr代码,对于字符串,它将从SQL查询中获取字符串。
这有什么来源吗

sd2nnvve

sd2nnvve1#

  1. QRCode.toDataURL是一个异步函数,但你试图使用同步。使用Promises和Async/Await代替回调函数。
    1.您发送的图像作为缓冲区,但它需要在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');
    });
});

...
kx5bkwkv

kx5bkwkv2#

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

f4t66c6m3#

这是关键!!
attachDataUrls:true,//接受消息中的base64内容

相关问题