Nodemailer: Unable to send embedded image

Viewed 49

I am following this tutorial to send an embedded image in an email using nodeMailer:

  const mailOptions = {
    from: "email@gmail.com",
    to: destination_email,
    subject: subject,
    html: html_email_content,
    attachments: [
      {
        filename: "image-2.png",
        path: __dirname + "/image-2.png",
        cid: "unique@kreata.ee", //same cid value as in the html img src
      },
    ],
  };

And in the html file I am using:

                <div>BEFORE IMAGE</div>
                <div>
                  <img src="cid:unique@kreata.ee" />
                </div>
                <div>AFTER IMAGE</div>

Note that the html file is read using this code and stored in html_email_content that's used in mailOptions:

const html_email_content =
  await emailTemplateGenerator.generateEmailTemplate(
    destination_name,
    destination_surname,
    description,
    button_label,
    button_url
  );

generateEmailTemplate definition:

var readHTMLFile = function (path) {
  return new Promise((resolve, reject) => {
    fs.readFile(path, { encoding: "utf-8" }, function (err, html) {
      if (err) {
        reject(err);
      } else {
        resolve(html);
      }
    });
  });
};

const generateEmailTemplate = async (
  destination_name,
  destination_surname,
  description,
  button_label,
  button_url
) => {
  const html_file = await readHTMLFile(__dirname + "/index.html");
  var template = handlebars.compile(html_file);
  var replacements = {
    destination_name,
    destination_surname,
    description,
    button_label,
    button_url,
  };
  var htmlToSend = template(replacements);
  return htmlToSend;
};

And this is the result:
enter image description here

The image does not get embedded.

0 Answers
Related