3

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);
  }
});
2
  • Your code looks correct except this line html: 'data' this should be html: data Commented Nov 27, 2018 at 9:43
  • You should require index.html, you should read that file through fs module. Commented Nov 27, 2018 at 9:45

2 Answers 2

4

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);
      }
    });
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Index.html file

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Node js Email </title>
        <link rel="stylesheet" href="">
      </head>


      <body>
        <div class="container"><br />
          <h1>Send Email</h1><br />
          <form onsubmit="sentthis()" method="post">
            <div class="divi">
                <label for="to"></label>To:</label>
                <input type="email" class="too" name="to">
              </div>


            <div class="divi">

                <label for="subject">Subject:</label>
                <input type="text" class="subjectt" name="subject">
              </div>
            <div class="divi">
                  <p>Body:</p>
                  <textarea cols="" rows="5"class="textarea" name="body"></textarea>
                </div>
            <div class="divi">
                <button type="submit" class="btn">Send</button>
              </div>
          </form>
      </div>
      </body>
    </html>

server.js file

    var express = require('express'),
        path = require('path'),
        nodeMailer = require('nodemailer'),
        bodyParser = require('body-parser');

        var app = express();
        app.engine('html', require('ejs').renderFile);
    app.set('view engine', 'html');
        app.use(express.static('public'));
        app.use(bodyParser.urlencoded({extended: true}));
        app.use(bodyParser.json());
        var port = 3000;
        app.get('/', function (req, res) {
          res.render('index.html');
        });
        app.post('/sent',function(req,res){
          let transporter = nodeMailer.createTransport({
              host: 'smtp.gmail.com',
              port: 465,
              secure: true,
              auth: {
                  user: '[email protected]',
                  pass: 'yourpassword'
              }
          });
          let mailOptions = {
              from: '"Marcus coffee" <[email protected]>', 
              to: "Receiver Name <[email protected]>",
              subject: req.body.subject, 
              text: req.body.body, 


          };

          transporter.sendMail(mailOptions, (error, info) => {
              if (error) {
                  return console.log(error);
              }
              console.log('Message  sent: ', info.messageId, info.response);
                  res.render('index.html');
              });
          });
              app.listen(port, function(){
                console.log('Server is running at port: ',port);
              });

This will solve your problem.

4 Comments

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
yes I was working on a project which called api on a web page and thats why I used app.post so to manage the routing procedure.
any ways the below provided solution is an individual script for sending email
if someone needs explaination how this code works just let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.