最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add image in my email, nodemailer and handlebars? - Stack Overflow

programmeradmin1浏览0评论

I am using nodemailer along with handlebars in my node project. The email is working fine but I am not able to attach image in the html template created by handlebars. I tried giving it in img src tag directly in the html but still not working. What i have is that the image is in svg form and in my assets folder of the project.

I also tried the example on the official nodemailer site which did not work as well. Please help me out!

/

This is my function which will be called when I get the request from the client.

sendEmail.js

var nodemailer = require("nodemailer");
const emailConfig = require("../readEmailConfigFile");

//reading username and password from json file
let fromemail = emailConfig.readFromEmail();
let password = emailConfig.readFromPassword();
var handlebars = require("handlebars");
var fs = require("fs");

const readHTMLFile = function(path, callback) {
  fs.readFile(path, { encoding: "utf-8" }, function(err, html) {
    if (err) {
      throw err;
      callback(err);
    } else {
      callback(null, html);
    }
  });
};

/* Method for sending Email */
const sendEmail = (details) => {
  var transporter = nodemailer.createTransport({
    service: "gmail",
    auth: {
      user: fromemail,
      pass: password
    }
  });

  readHTMLFile(
    __dirname + "/../emailTemplates/EmailTemplate.html",
    function(err, html) {
      var template = handlebarspile(html);

      var replacements = {
        firstName: details.firstName,
        lastName: details.lastName,
        address: details.address,
      };

      var htmlToSend = template(replacements);
      var mailOptions = {
        from: fromemail,
        to: details.email,
        subject: "ABC",
        html: htmlToSend
      };
      transporter.sendMail(mailOptions, function(error, info) {
        if (error) {
          console.log(error);
          callback(error);
        } else {
          console.log("Email Sent : " + info.response);
        }
      });
    }
  );
};

This is my html-template file

EmailTemplate.html

<html>
  <head>
    <title> </title>
  </head>
  <body>
    <p>
      Dear {{{firstName}}} {{{lastName}}}, your address is {{{address}}}</span
      >
    </p>
  </body>
</html>

I want to embed an svg image in this html!

I am using nodemailer along with handlebars in my node project. The email is working fine but I am not able to attach image in the html template created by handlebars. I tried giving it in img src tag directly in the html but still not working. What i have is that the image is in svg form and in my assets folder of the project.

I also tried the example on the official nodemailer site which did not work as well. Please help me out!

https://nodemailer./message/embedded-images/

This is my function which will be called when I get the request from the client.

sendEmail.js

var nodemailer = require("nodemailer");
const emailConfig = require("../readEmailConfigFile");

//reading username and password from json file
let fromemail = emailConfig.readFromEmail();
let password = emailConfig.readFromPassword();
var handlebars = require("handlebars");
var fs = require("fs");

const readHTMLFile = function(path, callback) {
  fs.readFile(path, { encoding: "utf-8" }, function(err, html) {
    if (err) {
      throw err;
      callback(err);
    } else {
      callback(null, html);
    }
  });
};

/* Method for sending Email */
const sendEmail = (details) => {
  var transporter = nodemailer.createTransport({
    service: "gmail",
    auth: {
      user: fromemail,
      pass: password
    }
  });

  readHTMLFile(
    __dirname + "/../emailTemplates/EmailTemplate.html",
    function(err, html) {
      var template = handlebars.pile(html);

      var replacements = {
        firstName: details.firstName,
        lastName: details.lastName,
        address: details.address,
      };

      var htmlToSend = template(replacements);
      var mailOptions = {
        from: fromemail,
        to: details.email,
        subject: "ABC",
        html: htmlToSend
      };
      transporter.sendMail(mailOptions, function(error, info) {
        if (error) {
          console.log(error);
          callback(error);
        } else {
          console.log("Email Sent : " + info.response);
        }
      });
    }
  );
};

This is my html-template file

EmailTemplate.html

<html>
  <head>
    <title> </title>
  </head>
  <body>
    <p>
      Dear {{{firstName}}} {{{lastName}}}, your address is {{{address}}}</span
      >
    </p>
  </body>
</html>

I want to embed an svg image in this html!

Share Improve this question asked May 1, 2019 at 14:59 Sourabh KarmarkarSourabh Karmarkar 871 silver badge7 bronze badges 1
  • 1 Maybe it didn't work beacuse svg is not supported by most of email clients. Try a simple png as like example in the official nodemailer docs. Check if it works. – Okan Kocyigit Commented May 1, 2019 at 15:05
Add a ment  | 

1 Answer 1

Reset to default 16

just add attachments parameter to your mailoptions object :

    var mailOptions = {
    from: fromemail,
    to: details.email,
    subject: "ABC",
    attachments: [{
        filename: 'imagename.svg',
          path: __dirname +'/assets/imagename.svg',
         cid: 'imagename'
  }],
    html: htmlToSend
  };

then add the img tag in your HTML <img src="cid:imagename">

发布评论

评论列表(0)

  1. 暂无评论