nodemailer-mailgun-transport
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -5,3 +5,3 @@ { | ||
"description": "A transport module to use with nodemailer to leverage Mailgun's REST API", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"repository": { | ||
@@ -40,2 +40,4 @@ "type": "git", | ||
"lodash.startswith": "^4.0.1", | ||
"async-series": "0.0.1", | ||
"consolidate": "^0.14.0", | ||
"mailgun-js": "0.7.10" | ||
@@ -42,0 +44,0 @@ }, |
@@ -57,2 +57,35 @@ nodemailer-mailgun-transport | ||
## Now with Consolidate.js templates | ||
If you pass a "template" key an object that contains a "name" key, an "engine" key and, optionally, a "context" object, you can use Handlebars templates to generate the HTML for your message. Like so: | ||
```javascript | ||
var handlebars = require('handlebars'); | ||
var contextObject = { | ||
variable1: 'value1', | ||
variable2: 'value2' | ||
}; | ||
nodemailerMailgun.sendMail({ | ||
from: 'myemail@example.com', | ||
to: 'recipient@domain.com', // An array if you have multiple recipients. | ||
subject: 'Hey you, awesome!', | ||
template: { | ||
name: 'email.hbs', | ||
engine: 'handlebars', | ||
context: contextObject | ||
} | ||
}, function (err, info) { | ||
if (err) { | ||
console.log('Error: ' + err); | ||
} | ||
else { | ||
console.log('Response: ' + info); | ||
} | ||
}); | ||
``` | ||
You can use any of the templating engines supported by [Consolidate.js](https://github.com/tj/consolidate.js/). Just require the engine module in your script, and pass a string of the engine name to the `template` object. Please see the Consolidate.js documentation for supported engines. | ||
**[1]** Quickly install dependencies | ||
@@ -63,2 +96,1 @@ ```bash | ||
``` | ||
'use strict'; | ||
var Mailgun = require('mailgun-js'); | ||
var cons = require('consolidate'); | ||
var packageData = require('../package.json'); | ||
var series = require('async-series'); | ||
var pickBy = require('lodash.pickby'); | ||
@@ -18,2 +20,3 @@ var some = require('lodash.some'); | ||
'attachment', | ||
'recipient-variables', | ||
'o:tag', | ||
@@ -51,31 +54,58 @@ 'o:campaign', | ||
MailgunTransport.prototype.send = function send(mail, callback) { | ||
var self = this; | ||
var mailData = mail.data; | ||
// convert nodemailer attachments to mailgun-js attachements | ||
if (mailData.attachments) { | ||
mailData.attachment = mailData.attachments.map(function (a) { | ||
return new self.mailgun.Attachment({ | ||
data: a.path || undefined, | ||
filename: a.filename || undefined, | ||
contentType: a.contentType || undefined, | ||
knownLength: a.knownLength || undefined | ||
series([ | ||
function(done) { | ||
if (mailData.template && mailData.template.name && mailData.template.engine) { | ||
mailData.template.context = mailData.template.context || {}; | ||
cons[mailData.template.engine](mailData.template.name, mailData.template.context, function(err, html) { | ||
if (err) throw err; | ||
mailData.html = html; | ||
done(); | ||
}); | ||
} else { | ||
done(); | ||
} | ||
}, | ||
function(done) { | ||
// convert nodemailer attachments to mailgun-js attachements | ||
if(mailData.attachment){ | ||
var a, b, data, aa = []; | ||
for(var i in mailData.attachment){ | ||
a = mailData.attachment[i]; | ||
// mailgunjs does not encode content string to a buffer | ||
if (typeof a.content === 'string') { | ||
data = new Buffer(a.content, a.encoding); | ||
} else { | ||
data = a.content || a.path || undefined; | ||
} | ||
b = new self.mailgun.Attachment({ | ||
data : data, | ||
filename : a.filename || undefined, | ||
contentType : a.contentType || undefined, | ||
knownLength : a.knownLength || undefined | ||
}); | ||
} | ||
} | ||
var options = pickBy(mailData, function (value, key) { | ||
if (whitelistExact.indexOf(key) !== -1) { | ||
return true; | ||
} | ||
return some(whitelistPrefix, function (prefix) { | ||
return startsWith(key, prefix); | ||
}); | ||
}); | ||
}); | ||
} | ||
var options = pickBy(mailData, function (value, key) { | ||
if (whitelistExact.indexOf(key) !== -1) { | ||
return true; | ||
self.messages.send(options, function (err, data) { | ||
callback(err || null, data); | ||
}); | ||
} | ||
return some(whitelistPrefix, function (prefix) { | ||
return startsWith(key, prefix); | ||
}); | ||
], function(err) { | ||
if (err) throw err; | ||
}); | ||
this.messages.send(options, function (err, data) { | ||
callback(err || null, data); | ||
}); | ||
}; |
@@ -30,3 +30,3 @@ var chai = require('chai'); | ||
html: '<b>Hello</b>', | ||
attachments: [], | ||
attachment: [], | ||
'o:tag': 'Tag', | ||
@@ -82,3 +82,3 @@ 'o:campaign': 'Campaign', | ||
text: 'Hello', | ||
attachments: [{ | ||
attachment: [{ | ||
path: '/', | ||
@@ -97,3 +97,3 @@ filename: 'CONTRIBUTORS.md', | ||
var attachment = call.args[0].attachment[0]; | ||
expect(attachment.data).to.equal('/'); | ||
expect(attachment.path).to.equal('/'); | ||
expect(attachment.filename).to.equal('CONTRIBUTORS.md'); | ||
@@ -131,2 +131,33 @@ expect(attachment.contentType).to.equal('text/markdown'); | ||
}); | ||
}); | ||
describe('when referencing a template file', function() { | ||
it('should insert variables and send the data as HTML', function() { | ||
var self = this; | ||
var data = { | ||
from: 'from@bar.com', | ||
to: 'to@bar.com', | ||
subject: 'Subject', | ||
template: { | ||
name: 'test_template.hbs', | ||
engine: 'handlebars', | ||
context: { | ||
variable1: 'Passed!' | ||
} | ||
}, | ||
foo: 'bar' | ||
}; | ||
this.transport.send({ | ||
data: data | ||
}, function () { | ||
expect(self.transport.messages.send).to.have.been.calledWith({ | ||
from: 'from@bar.com', | ||
to: 'to@bar.com', | ||
subject: 'Subject', | ||
html: '<body><h1>Passed!</h1></body>' | ||
}); | ||
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
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
13868
9
247
95
0
6
+ Addedasync-series@0.0.1
+ Addedconsolidate@^0.14.0
+ Addedasync-series@0.0.1(transitive)
+ Addedbluebird@3.7.2(transitive)
+ Addedconsolidate@0.14.5(transitive)