带有qrcode图像的Nodemailer主体

时间:2019-03-11 09:55:46

标签: node.js nodemailer

我正在使用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是qr代码,对于字符串,它将从SQL查询中获取字符串。

有任何来源吗?

2 个答案:

答案 0 :(得分:1)

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

...

答案 1 :(得分:0)

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