In the earlier posts, I had explained how to send emails in CodeIgniter. Now I would like to explain how to send HTML template emails using SMTP in NodeJS.
Create HTML file with inline styles, In the earlier posts, I had explained how to use the HTML tags in the page. So prepare the HTML file.
HTML template file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <style type="text/css"> /* ------------------------------------- GLOBAL ------------------------------------- */ * { margin: 0; padding: 0; font-family: "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; } body { -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6; }<br /> p{text-align:left;} </style> <div> Dear ##firstname, It is a demo for Sending HTML template mails using SMTP in NodeJS Developed by Anil Kumar Panigrahi (Anil Labs Founder) </div> |
Now we will go how to include it into NodeJS
Include the node module
nodemailer
In the NodeJS file like
1 | var nodemailer = require('nodemailer'); |
Include the SMTP details in a config file
1 2 3 4 5 6 7 8 9 | module.exports = { Service: 'GoDaddy/Gmail/AWS', Host: '', //depends on service Username: '', Password: '', Port: '', //Port depends on service type From: '', //From Email address FromName: '', //From Name } |
In a main file include node module and mail settings
1 2 3 4 | var nodemailer = require('nodemailer'); var mailset = require('../config/mailsettings.js'); var fs = require("fs"); var path = require('path'); |
Create function to object for emails
1 2 3 4 5 6 7 8 9 10 11 12 13 | function generateTransporter(){ var transporter = nodemailer.createTransport("SMTP", { port: mailset.Port, host: mailset.Host, secure: true, auth: { user: mailset.Username, pass: mailset.Password }, debug: true }); return transporter; } |
For Gmail SMTP
1 2 3 4 5 6 7 8 9 10 | function generateTransporter(){ var transporter = nodemailer.createTransport("SMTP", { service: "Gmail", auth: { user: mailset.Username, pass: mailset.Password } }); return transporter; } |
In function where we use the object to send emails
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | var filePath = path.join(__dirname, '../templates/email.html'); var transporter = generateTransporter(); var FirstName = "Anil"; fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ data = data.toString(); data = data.replace(/##firstname/g, FirstName); var mailOptions = { from: mailset.FromName+'<'+mailset.From+'>', // sender address to: toemail, // list of receivers subject: 'Sending HTML template mails using SMTP in NodeJS', // Subject line html: data // html body }; // send mail with defined transport object transporter.sendMail(mailOptions, function(error, info){ if(error){ return console.log(error); } if(error){ response = {"Status" : "Failed"}; } else{ response = {"Status" : "Success"}; } res.json(response); }); }); |
Hope it will be useful. Please let me know if you face issues with it.
1 Comment
Sending the emails with attachment in Node.js - Anil Labs · December 10, 2019 at 7:03 am
[…] the earlier post we have discussed about Sending HTML template emails using SMTP in NodeJS , now we will learn about how to sending the emails with attachment in Node.js along with with HTML […]