Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTML template through mail using node js node mailer

Tags:

html

node.js

I had written node mailer code from w3schools. I want to send the html template designed (index.html in my case). Below is the code. please help me how can i send html template to the mail using node js.

var nodemailer = require('nodemailer');
var data = require('index.html');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: '[email protected]',
    pass: 'yourpassword'
  }
});

var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Sending Email using Node.js',
  html: 'data'
};

transporter.sendMail(mailOptions, function(error, info) {
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});
like image 327
Shaik Avatar asked Mar 05 '26 13:03

Shaik


1 Answers

This is the right way of passing html in the nodemailer

var nodemailer = require('nodemailer');
var fs = require('fs');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: '[email protected]',
    pass: 'yourpassword'
  }
});


fs.readFile('index.html', {encoding: 'utf-8'}, function (err, html) {
  if (err) {
    console.log(err);
  } else {
    var mailOptions = {
      from: '[email protected]',
      to: '[email protected]',
      subject: 'Sending Email using Node.js',
      html: html
    };
    transporter.sendMail(mailOptions, function(error, info) {
      if (error) {
        console.log(error);
      } else {
        console.log('Email sent: ' + info.response);
      }
    });
  }
});
like image 197
Aabid Avatar answered Mar 08 '26 05:03

Aabid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!