html 中的图像在通过 nodemailer -node.js 作为电子邮件发送时不显示?

the image in html in not displaying when sent as email through nodemailer -node.js?

我正在 node.js 中使用 nodemailer 发送 html 电子邮件,我正在使用 email-templates npm 包的模板概念发送 [https://www.npmjs.com/package/email-templates].

此处,邮件发送成功,但HTML中的图片在收到时未显示在邮件中。

这是发送邮件的主文件:

const nodemailer = require("nodemailer");
var Email = require('email-templates');
const path = require('path');

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: 'test@gmail.com',
      pass: 'password'
    }
  });

  const email = new Email({
    transport: transporter,
    send: true,
    preview: false,
    views: {
        options: {
          extension: 'ejs' 
        }
      },
    juice: true,
        juiceResources: {
            preserveImportant: true,
                webResources: {
                    relativeTo: path.join(__dirname,'./templates/emailCampaign/images')
            }
    }
  });

  email.send({
    template:path.join(__dirname,'./templates/emailCampaign'),
    message: {
      from: 'test@gmail.com',
      to: 'test@gmail.com',
    },
    locals: {
        tournament_name:"tournament_name",
        date:"date",
        time:"time",
        fee:"4",
        sections:"sections",
        description:"description"
    },


  }).then(() => console.log('email has been sent!'));

这是 HTML 我希望显示图像的部分

<img style="width: 100%;
                height: auto;"
        src="./images/background.jpg" 
        alt="game">

您可以这样做:

  email.send({
    template:path.join(__dirname,'./templates/emailCampaign'),
    message: {
      from: 'test@gmail.com',
      to: 'test@gmail.com',
    },
    locals: {
        tournament_name:"tournament_name",
        date:"date",
        time:"time",
        fee:"4",
        sections:"sections",
        description:"description"
    },
    attachments: [{
                filename: 'logo.jpg',
                path: `${__dirname}/../public/images/logo.jpg`,
                cid: 'logo1' //same cid value as in the html img src
   }]

然后在您的 html img src 属性中,具有相同的 cid 值,如下所示:

<img src="cid:logo1"/>

Check the documentation here