Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nodemailer-express-handlebars

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nodemailer-express-handlebars - npm Package Compare versions

Comparing version 6.0.0 to 6.1.0

.prettierrc.js

60

lib/generator.js
'use strict';
var path = require('path'),
handlebars = require('express-handlebars');
const path = require('path');
const handlebars = require('express-handlebars');
var TemplateGenerator = function(opts) {
var viewEngine = opts.viewEngine || {};
class TemplateGenerator {
constructor(opts) {
let viewEngine = opts.viewEngine || {};
if (!viewEngine.renderView) {
viewEngine = handlebars.create(viewEngine);
viewEngine = handlebars.create(viewEngine);
}

@@ -14,27 +15,34 @@ this.viewEngine = viewEngine;

this.extName = opts.extName || '.handlebars';
};
}
TemplateGenerator.prototype.render = function render(mail, cb) {
if (mail.data.html) return cb();
async render(mail) {
if (mail.data.html) {
return;
}
var templatePath = path.join(this.viewPath, mail.data.template + this.extName);
var textTemplatePath='';
if (!!mail.data.text_template) {
textTemplatePath=path.join(this.viewPath, mail.data.text_template + this.extName);
let templatePath = path.join(
this.viewPath,
mail.data.template + this.extName
);
let textTemplatePath = '';
if (mail.data.text_template) {
textTemplatePath = path.join(
this.viewPath,
mail.data.text_template + this.extName
);
}
var viewEngine=this.viewEngine;
this.viewEngine.renderView(templatePath, mail.data.context, function(err, body) {
if (err) return cb(err);
mail.data.html = body;
if(!mail.data.text_template)cb();
else{
viewEngine.renderView(textTemplatePath, mail.data.context, function(err, body) {
if (err) return cb(err);
mail.data.text = body;
cb();
});
}
});
};
mail.data.html = await this.viewEngine.renderView(
templatePath,
mail.data.context
);
if (mail.data.text_template) {
mail.data.text = await this.viewEngine.renderView(
textTemplatePath,
mail.data.context
);
}
}
}
module.exports = TemplateGenerator;

@@ -1,11 +0,16 @@

'use strict';
const TemplateGenerator = require('./generator');
var TemplateGenerator = require('./generator');
module.exports = function (options) {
const generator = new TemplateGenerator(options);
module.exports = function(options) {
var generator = new TemplateGenerator(options);
return async (mail, cb) => {
try {
await generator.render(mail);
} catch (err) {
return cb(err);
}
return function(mail, cb) {
generator.render(mail, cb);
};
};
cb();
};
};
{
"name": "nodemailer-express-handlebars",
"version": "6.0.0",
"version": "6.1.0",
"description": "A plugin for nodemailer that uses express-handlebars view engine to generate emails",

@@ -23,10 +23,11 @@ "main": "index.js",

"license": "MIT",
"dependencies": {
"express-handlebars": "^6.0.0"
"peerDependencies": {
"express-handlebars": ">= 6.0.0",
"nodemailer": ">= 6.0.0"
},
"devDependencies": {
"chai": "^4.1.2",
"express-handlebars": "^6.0.0",
"mocha": "^10.0.0",
"nodemailer": "^6.4.6",
"nodemailer-stub-transport": "^1.1.0"
"nodemailer": "^6.4.6"
},

@@ -33,0 +34,0 @@ "engines": {

@@ -12,7 +12,7 @@ # Express Handlebars plugin for Nodemailer

//reference the plugin
var hbs = require('nodemailer-express-handlebars');
const hbs = require('nodemailer-express-handlebars');
//attach the plugin to the nodemailer transporter
transporter.use('compile', hbs(options));
//send mail with options
var mail = {
const mail = {
from: 'from@domain.com',

@@ -28,2 +28,16 @@ to: 'to@domain.com',

```
You can send a multipart html and text email by setting the `text_template` option on a mail message.
```javascript
const mail = {
from: 'from@domain.com',
to: 'to@domain.com',
subject: 'Test',
template: 'email',
text_template: 'text',
context: {
name: 'Name'
}
}
```
## Plugin Options

@@ -30,0 +44,0 @@ The plugin expects the following options:

'use strict';
var should = require('chai').should();
var nodemailerExpressHandlebars = require('../..'),
nodemailer = require('nodemailer'),
transport = require('nodemailer-stub-transport'),
handlebars = require('express-handlebars'),
path = require('path');
const { expect } = require('chai');
const nodemailerExpressHandlebars = require('../..'),
nodemailer = require('nodemailer'),
handlebars = require('express-handlebars'),
path = require('path');
describe('when view engine passed', function() {
describe('when view engine passed', function () {
let sut, mail, transporter, viewEngine;
var sut,
mail,
transporter,
viewEngine;
beforeEach(function () {
transporter = nodemailer.createTransport({
streamTransport: true,
buffer: true,
});
viewEngine = handlebars.create({
partialsDir: path.resolve(__dirname, '../views/partials/'),
defaultLayout: false,
});
sut = nodemailerExpressHandlebars({
viewEngine: viewEngine,
viewPath: path.resolve(__dirname, '../views'),
});
transporter.use('compile', sut);
mail = {
from: 'from@domain.com',
to: 'to@domain.com',
subject: 'Test',
template: 'email',
context: {
name: 'Name',
},
};
});
beforeEach(function() {
transporter = nodemailer.createTransport(transport());
viewEngine = handlebars.create({
partialsDir: 'partials/',
defaultLayout: false
});
sut = nodemailerExpressHandlebars({
viewEngine: viewEngine,
viewPath: path.resolve(__dirname, '../views')
});
transporter.use('compile', sut);
mail = {
from: 'from@domain.com',
to: 'to@domain.com',
subject: 'Test',
template: 'email',
context: {
name: 'Name'
}
};
it('should handle errors', function (done) {
viewEngine.renderView = () => {
throw 'Rendering Error';
};
transporter.sendMail(mail, (err, info) => {
expect(err).to.eq('Rendering Error');
done();
});
});
it('should set html on email', function(done) {
it('should set html on email', function (done) {
transporter.sendMail(mail, (err, info) => {
if (err) return done(err);
transporter.sendMail(mail, function(err, info) {
if (err) return done(err);
const body = info.message.toString();
expect(body).to.contain('<h1>This is a test</h1>');
expect(body).to.contain('Name');
done();
});
});
var body = info.response.toString();
body.should.contain('<h1>This is a test</h1>');
body.should.contain('Name');
done();
});
it('should not overwrite existing html entry', function (done) {
const html = (mail.html = '<h1>hardcoded</h1>');
transporter.sendMail(mail, (err, info) => {
if (err) return done(err);
const body = info.message.toString();
expect(body).to.contain(html);
done();
});
});
it('should not overwrite existing html entry', function(done) {
it('should handle text_template', function (done) {
mail.text_template = 'text';
transporter.sendMail(mail, (err, info) => {
if (err) return done(err);
var html = mail.html = '<h1>hardcoded</h1>';
transporter.sendMail(mail, function(err, info) {
if (err) return done(err);
const body = info.message.toString();
expect(body).to.contain('<h1>This is a test</h1>');
expect(body).to.contain('Name');
expect(body).to.contain('Text email');
var body = info.response.toString();
body.should.contain(html);
done();
});
done();
});
});
it('should handle view and partials', function (done) {
mail.template = 'with_partial';
transporter.sendMail(mail, (err, info) => {
if (err) return done(err);
const body = info.message.toString();
expect(body).to.contain('<h1>Header</h1>');
expect(body).to.contain('Email content');
done();
});
});
});
describe('when options passed', function () {
var sut, mail, transporter;
describe('when options passed', function() {
var sut,
mail,
transporter;
beforeEach(function() {
transporter = nodemailer.createTransport(transport());
sut = nodemailerExpressHandlebars({
viewEngine: {
partialsDir: 'partials/',
defaultLayout: false
},
viewPath: path.resolve(__dirname, '../views')
});
transporter.use('compile', sut);
mail = {
from: 'from@domain.com',
to: 'to@domain.com',
subject: 'Test',
template: 'email',
context: {
name: 'Name'
}
};
beforeEach(function () {
transporter = nodemailer.createTransport({
streamTransport: true,
buffer: true,
});
sut = nodemailerExpressHandlebars({
viewEngine: {
partialsDir: path.resolve(__dirname, '../views/partials/'),
defaultLayout: false,
},
viewPath: path.resolve(__dirname, '../views'),
});
transporter.use('compile', sut);
mail = {
from: 'from@domain.com',
to: 'to@domain.com',
subject: 'Test',
template: 'email',
context: {
name: 'Name',
},
};
});
it('should set html on email', function(done) {
it('should set html on email', function (done) {
transporter.sendMail(mail, (err, info) => {
if (err) return done(err);
transporter.sendMail(mail, function(err, info) {
if (err) return done(err);
var body = info.response.toString();
body.should.contain('<h1>This is a test</h1>');
body.should.contain('Name');
done();
});
const body = info.message.toString();
expect(body).to.contain('<h1>This is a test</h1>');
expect(body).to.contain('Name');
done();
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc