-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from yads/rewrite-in-modern-es
major code rewrite
- Loading branch information
Showing
10 changed files
with
216 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
singleQuote: true, | ||
overrides: [ | ||
{ | ||
files: '*.hbs', | ||
options: { | ||
singleQuote: false, | ||
parser: 'glimmer', | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,11 @@ npm install nodemailer-express-handlebars | |
# Usage | ||
```javascript | ||
//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: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Test', | ||
|
@@ -24,6 +24,20 @@ var mail = { | |
} | ||
transporter.sendMail(mail); | ||
``` | ||
|
||
You can send a multipart html and text email by setting the `text_template` option on a mail message. | ||
```javascript | ||
const mail = { | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Test', | ||
template: 'email', | ||
text_template: 'text', | ||
context: { | ||
name: 'Name' | ||
} | ||
} | ||
``` | ||
## Plugin Options | ||
The plugin expects the following options: | ||
* __viewEngine (required)__ either the express-handlebars view engine instance or [options for the view engine](https://github.com/express-handlebars/express-handlebars#configuration-and-defaults) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,47 @@ | ||
'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); | ||
} | ||
this.viewEngine = viewEngine; | ||
this.viewPath = opts.viewPath; | ||
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; | ||
} | ||
|
||
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 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); | ||
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 | ||
); | ||
} | ||
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(); | ||
}); | ||
} | ||
}); | ||
}; | ||
} | ||
} | ||
|
||
module.exports = TemplateGenerator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,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); | ||
} | ||
|
||
cb(); | ||
}; | ||
}; | ||
|
||
return function(mail, cb) { | ||
generator.render(mail, cb); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,134 @@ | ||
'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'); | ||
|
||
describe('when view engine passed', function() { | ||
|
||
var sut, | ||
mail, | ||
transporter, | ||
viewEngine; | ||
|
||
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: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Test', | ||
template: 'email', | ||
context: { | ||
name: 'Name' | ||
} | ||
}; | ||
const { expect } = require('chai'); | ||
const nodemailerExpressHandlebars = require('../..'), | ||
nodemailer = require('nodemailer'), | ||
handlebars = require('express-handlebars'), | ||
path = require('path'); | ||
|
||
describe('when view engine passed', function () { | ||
let 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: '[email protected]', | ||
to: '[email protected]', | ||
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) { | ||
|
||
var html = mail.html = '<h1>hardcoded</h1>'; | ||
transporter.sendMail(mail, function(err, info) { | ||
if (err) return done(err); | ||
it('should handle text_template', function (done) { | ||
mail.text_template = 'text'; | ||
transporter.sendMail(mail, (err, info) => { | ||
if (err) return done(err); | ||
|
||
var body = info.response.toString(); | ||
body.should.contain(html); | ||
done(); | ||
}); | ||
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'); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should handle view and partials', function (done) { | ||
mail.template = 'with_partial'; | ||
transporter.sendMail(mail, (err, info) => { | ||
if (err) return done(err); | ||
|
||
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: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Test', | ||
template: 'email', | ||
context: { | ||
name: 'Name' | ||
} | ||
}; | ||
const body = info.message.toString(); | ||
expect(body).to.contain('<h1>Header</h1>'); | ||
expect(body).to.contain('Email content'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should set html on email', function(done) { | ||
|
||
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(); | ||
}); | ||
describe('when options passed', function () { | ||
var sut, mail, transporter; | ||
|
||
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: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Test', | ||
template: 'email', | ||
context: { | ||
name: 'Name', | ||
}, | ||
}; | ||
}); | ||
|
||
it('should set html on email', function (done) { | ||
transporter.sendMail(mail, (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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Header</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Text email | ||
{{name}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{{> header}} | ||
Email content |
Oops, something went wrong.