nodemailer-express-handlebars
Advanced tools
Comparing version 6.0.0 to 6.1.0
'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(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8878
13
183
54
2
1
+ Added@isaacs/cliui@8.0.2(transitive)
+ Addedansi-regex@5.0.16.1.0(transitive)
+ Addedansi-styles@4.3.06.2.1(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcross-spawn@7.0.6(transitive)
+ Addedeastasianwidth@0.2.0(transitive)
+ Addedemoji-regex@8.0.09.2.2(transitive)
+ Addedexpress-handlebars@8.0.1(transitive)
+ Addedforeground-child@3.3.0(transitive)
+ Addedglob@11.0.0(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjackspeak@4.0.2(transitive)
+ Addedlru-cache@11.0.2(transitive)
+ Addedminimatch@10.0.1(transitive)
+ Addedminipass@7.1.2(transitive)
+ Addednodemailer@6.9.16(transitive)
+ Addedpackage-json-from-dist@1.0.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpath-scurry@2.0.0(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedsignal-exit@4.1.0(transitive)
+ Addedstring-width@4.2.35.1.2(transitive)
+ Addedstrip-ansi@6.0.17.1.0(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwrap-ansi@7.0.08.1.0(transitive)
- Removedexpress-handlebars@^6.0.0
- Removedexpress-handlebars@6.0.7(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@8.1.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedminimatch@5.1.6(transitive)
- Removedonce@1.4.0(transitive)
- Removedwrappy@1.0.2(transitive)