0

I want to send Html Page using nodemailer in Email. But I can't include my html page. How can is this possible? Please help and give me a solution...

This is my EmailSendHtml.js File

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

 var d;

 fs.readFile('index.html','UTF-8',function(err,data)
 {
     d=data;
 });

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

var a='<h1>Welcome</h1><p>That was easy!</p>';

 var mailOptions={
   from:'[email protected]',
   to:'[email protected]',
   subject:'Sending HTML page using Node JS',
   html:d
 };

 transporter.sendMail(mailOptions,function(error,info)
 {
    if(error)
    {
        console.log(error);
    }
    else
    {
        console.log('Email Sent: '+info.response);
    }
 });

That's my html page which I want to include and show in email.

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link type="text/css" rel="stylesheet" href="style.css"/>
    </head>
    <body>
        <h1>TODO write content</h1>
    </body>
</html>

This is my style.css file

/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
*/
/* 
    Created on : Feb 19, 2018, 2:03:50 PM
    Author     : Lenovo
*/

h1
{
    color:red;
}
1
  • fs.readFile() is async. Commented Feb 19, 2018 at 9:31

2 Answers 2

1
  • Use inline css in HTML and
  • Use file read stream as below
  • pass the file read stream to sendmail // { html }
  • Don't forget to explicitly close the file stream

Code sample

var htmlstream = fs.createReadStream("index.html");

transport.sendMail({ html: htmlstream }, function(err) {
    if (err) {
        // check if htmlstream is still open and close it to clean up
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Just change your code as following :

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

require.extensions['.html'] = function (module, filename) {
    module.exports = fs.readFileSync(filename, 'utf8');
};

 var data = require('index.html'); // path to your HTML template

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

var a='<h1>Welcome</h1><p>That was easy!</p>';

 var mailOptions={
   from:'[email protected]',
   to:'[email protected]',
   subject:'Sending HTML page using Node JS',
   html:data
 };

 transporter.sendMail(mailOptions,function(error,info)
 {
    if(error)
    {
        console.log(error);
    }
    else
    {
        console.log('Email Sent: '+info.response);
    }
 });

1 Comment

I can't see the html page in email

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.