Skip to content

Commit

Permalink
node-sparkpost 2.0.1 (#15)
Browse files Browse the repository at this point in the history
* Updated for node-sparkpost 2.0.0

* Removed node 0.12, 0.10 from travis, included stackIdentity string

* Added CC, BCC support and much delinting

* node-sparkpost -> 2.1.0
  • Loading branch information
ewandennis authored and aydrian committed Jan 6, 2017
1 parent eb0597c commit a20695a
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 101 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ language: node_js
node_js:
- '4.1'
- '4.0'
- '0.12'
- '0.10'
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Populate node-sparkpost's stackIdentity option

### Updated
- Updated to `[email protected]`.

## [1.1.0] - 2016/11/10
### Added
Expand Down
22 changes: 11 additions & 11 deletions examples/sendmail.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';

/* eslint-disable no-console */
var nodemailer = require('nodemailer')
, sparkPostTransport = require('nodemailer-sparkpost-transport')
, transporter = nodemailer.createTransport(sparkPostTransport({
'sparkPostApiKey': '<YOUR_API_KEY>',
'options': {
'open_tracking': true,
'click_tracking': true,
'transactional': true
},
'campaign_id': 'Nodemailer Demo'
}));
const nodemailer = require('nodemailer');
const sparkPostTransport = require('nodemailer-sparkpost-transport');
const transporter = nodemailer.createTransport(sparkPostTransport({
'sparkPostApiKey': '<YOUR_API_KEY>',
'options': {
'open_tracking': true,
'click_tracking': true,
'transactional': true
},
'campaign_id': 'Nodemailer Demo'
}));

transporter.sendMail({
from: '[email protected]',
Expand Down
70 changes: 40 additions & 30 deletions lib/sparkPostTransport.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

// Dependencies
var pkg = require('../package')
, SparkPost = require('sparkpost');
const pkg = require('../package');
const SparkPost = require('sparkpost');

// Constructor
function SparkPostTransport(options) {
var opt;
let opt;

// Set required properties
this.name = 'SparkPost';
Expand All @@ -15,7 +15,9 @@ function SparkPostTransport(options) {

// Set the SparkPost API Key (must have appropriate Transmission resource permissions)
this.sparkPostApiKey = process.env.SPARKPOST_API_KEY || options.sparkPostApiKey;
this.sparkPostEmailClient = new SparkPost(this.sparkPostApiKey);
this.sparkPostEmailClient = new SparkPost(this.sparkPostApiKey, {
stackIdentity: `nodemailer-sparkpost-transport/${this.version}`
});

// Set any options which are valid
for (opt in options) {
Expand All @@ -26,8 +28,8 @@ function SparkPostTransport(options) {
}

function populateCustomFields(message, defaults, request) {
var data = message.data
, customFields = ['campaign_id', 'metadata', 'substitution_data', 'options', 'content', 'recipients'];
const data = message.data;
const customFields = ['campaign_id', 'metadata', 'substitution_data', 'options', 'content', 'recipients'];

// Apply default SP-centric options and override if provided in mail object
customFields.forEach(function(fld) {
Expand All @@ -53,13 +55,10 @@ function populateFrom(inreq, outreq) {
}

function populateInlineStdFields(message, resolveme, request) {
var data = message.data;
const data = message.data;

populateFrom(data, request);

// cc
// bcc

if (data.subject) {
request.content.subject = data.subject;
}
Expand All @@ -76,19 +75,31 @@ function populateInlineStdFields(message, resolveme, request) {
// headers
}

function populateRecipients(request, msgData) {
if (msgData.to) {
request.recipients = emailList(msgData.to) || [];
}

if (msgData.cc) {
request.cc = emailList(msgData.cc);
}

if (msgData.bcc) {
request.bcc = emailList(msgData.bcc);
}
}

SparkPostTransport.prototype.send = function send(message, callback) {
var data = message.data
, request = {
content: {}
}
, resolveme = {};
const data = message.data;
const request = {
content: {}
};
const resolveme = {};

// Conventional nodemailer fields override SparkPost-specific ones and defaults
populateCustomFields(message, this, request);

if (data.to) {
request.recipients = emailList(data.to) || [];
}
populateRecipients(request, data);

if (data.raw) {
resolveme.raw = 'email_rfc822';
Expand All @@ -100,17 +111,15 @@ SparkPostTransport.prototype.send = function send(message, callback) {
};

SparkPostTransport.prototype.resolveAndSend = function(mail, toresolve, request, callback) {
var self = this
, keys = Object.keys(toresolve)
, srckey
, dstkey;
const self = this;
const keys = Object.keys(toresolve);

if (keys.length === 0) {
return this.sendWithSparkPost(request, callback);
}

srckey = keys[0];
dstkey = toresolve[keys[0]];
const srckey = keys[0];
const dstkey = toresolve[keys[0]];

delete toresolve[srckey];

Expand All @@ -121,7 +130,7 @@ SparkPostTransport.prototype.resolveAndSend = function(mail, toresolve, request,
};

SparkPostTransport.prototype.loadContent = function(mail, key, callback) {
var content = mail.data[key];
const content = mail.data[key];
if (typeof content === 'string') {
return process.nextTick(function() {
callback(null, content);
Expand All @@ -136,22 +145,22 @@ SparkPostTransport.prototype.loadContent = function(mail, key, callback) {
};

SparkPostTransport.prototype.sendWithSparkPost = function(transBody, callback) {
this.sparkPostEmailClient.transmissions.send({transmissionBody: transBody}, function(err, res) {
this.sparkPostEmailClient.transmissions.send(transBody, function(err, res) {
if (err) {
return callback(err);
}
// Example successful Sparkpost transmission response:
// { "results": { "total_rejected_recipients": 0, "total_accepted_recipients": 1, "id": "66123596945797072" } }
return callback(null, {
messageId: res.body.results.id,
accepted: res.body.results.total_accepted_recipients,
rejected: res.body.results.total_rejected_recipients
messageId: res.results.id,
accepted: res.results.total_accepted_recipients,
rejected: res.results.total_rejected_recipients
});
});
};

function emailList(strOrLst) {
var lst = strOrLst;
let lst = strOrLst;
if (typeof strOrLst === 'string') {
lst = strOrLst.split(',');
}
Expand All @@ -171,3 +180,4 @@ function emailList(strOrLst) {
module.exports = function(options) {
return new SparkPostTransport(options);
};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"homepage": "https://github.com/SparkPost/nodemailer-sparkpost-transport",
"dependencies": {
"sparkpost": "^1.3.8"
"sparkpost": "^2.1.0"
},
"devDependencies": {
"chai": "^3.5.0",
Expand Down
Loading

0 comments on commit a20695a

Please sign in to comment.