-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added CC and BCC sugar #194
Changes from all commits
ffbf262
4223714
8e72004
248ca07
23164f6
4b304a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
var key = 'YOURAPIKEY' | ||
, SparkPost = require('sparkpost') | ||
, client = new SparkPost(key) | ||
, transmission = { | ||
recipients: [ | ||
{ | ||
address: { | ||
email: 'original.recipient@example.com', | ||
name: 'Original Recipient' | ||
}, | ||
substitution_data: { | ||
recipient_type: 'Original' | ||
} | ||
} | ||
], | ||
bcc: [ | ||
{ | ||
address: { | ||
email: 'bcc.recipient@example.com', | ||
}, | ||
substitution_data: { | ||
recipient_type: 'BCC' | ||
} | ||
} | ||
], | ||
content: { | ||
from: { | ||
name: 'Node BCC Test', | ||
email: 'from@example.com' | ||
}, | ||
subject: 'Example email using bcc', | ||
text: 'An example email using bcc with SparkPost to the {{recipient_type}} recipient.', | ||
html: '<p>An example email using bcc with SparkPost to the {{recipient_type}} recipient.</p>' | ||
} | ||
}; | ||
|
||
// Promise | ||
client.transmissions.send(transmission) | ||
.then(data => { | ||
console.log('Congrats! You sent an email with bcc using SparkPost!'); | ||
console.log(data); | ||
}) | ||
.catch(err => { | ||
console.log('Whoops! Something went wrong'); | ||
console.log(err); | ||
}); | ||
|
||
// Callback | ||
client.transmissions.send(transmission, function(err, data) { | ||
if (err) { | ||
console.log('Whoops! Something went wrong'); | ||
console.log(err); | ||
} else { | ||
console.log('Congrats! You sent an email with bcc using SparkPost!'); | ||
console.log(data); | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
var key = 'YOURAPIKEY' | ||
, SparkPost = require('sparkpost') | ||
, client = new SparkPost(key) | ||
, transmission = { | ||
recipients: [ | ||
{ | ||
address: { | ||
email: 'original.recipient@example.com', | ||
name: 'Original Recipient' | ||
}, | ||
substitution_data: { | ||
recipient_type: 'Original' | ||
} | ||
}, | ||
], | ||
cc: [ | ||
{ | ||
address: { | ||
email: 'cc.recipient@example.com', | ||
name: 'Carbon Copy Recipient', | ||
}, | ||
substitution_data: { | ||
recipient_type: 'CC' | ||
} | ||
} | ||
], | ||
content: { | ||
from: { | ||
name: 'Node CC Test', | ||
email: 'from@example.com' | ||
}, | ||
subject: 'Example email using cc', | ||
text: 'An example email using cc with SparkPost to the {{recipient_type}} recipient.', | ||
html: '<p>An example email using cc with SparkPost to the {{recipient_type}} recipient.</p>' | ||
} | ||
}; | ||
|
||
// Promise | ||
client.transmissions.send(transmission) | ||
.then(data => { | ||
console.log('Congrats! You sent an email with cc using SparkPost!'); | ||
console.log(data); | ||
}) | ||
.catch(err => { | ||
console.log('Whoops! Something went wrong'); | ||
console.log(err); | ||
}); | ||
|
||
// Callback | ||
client.transmissions.send(transmission, function(err, data) { | ||
if (err) { | ||
console.log('Whoops! Something went wrong'); | ||
console.log(err); | ||
} else { | ||
console.log('Congrats! You sent an email with cc using SparkPost!'); | ||
console.log(data); | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const api = 'transmissions'; | ||
const Promise = require('./Promise'); | ||
|
||
|
@@ -59,7 +60,6 @@ module.exports = function(client) { | |
* @returns {Promise} | ||
*/ | ||
send: function(transmission, options, callback) { | ||
|
||
// Handle optional options argument | ||
if (typeof options === 'function') { | ||
callback = options; | ||
|
@@ -70,6 +70,8 @@ module.exports = function(client) { | |
return Promise.reject(new Error('transmission object is required')).asCallback(callback); | ||
} | ||
|
||
transmission = formatPayload(transmission); | ||
|
||
const reqOpts = { | ||
uri: api, | ||
json: transmission, | ||
|
@@ -81,3 +83,95 @@ module.exports = function(client) { | |
}; | ||
|
||
}; | ||
|
||
function formatPayload(originalTransmission) { | ||
const transmission = _.cloneDeep(originalTransmission); | ||
|
||
// don't format the payload if we are not given an array of recipients | ||
if (!_.isArray(transmission.recipients)) { | ||
return transmission; | ||
} | ||
|
||
// format all the original recipients to be in the object format | ||
transmission.recipients = _.map(transmission.recipients, (recipient) => { | ||
recipient.address = addressToObject(recipient.address); | ||
|
||
return recipient; | ||
}); | ||
|
||
// add the CC headers | ||
if (_.isArray(transmission.cc)) { | ||
_.set(transmission, 'content.headers.CC', generateCCHeader(transmission)); | ||
} | ||
|
||
const headerTo = generateHeaderTo(transmission.recipients); | ||
|
||
transmission.recipients = addListToRecipients(transmission, 'cc', headerTo); | ||
transmission.recipients = addListToRecipients(transmission, 'bcc', headerTo); | ||
|
||
delete transmission.cc; | ||
delete transmission.bcc; | ||
|
||
return transmission; | ||
} | ||
|
||
function addListToRecipients(transmission, listName, headerTo) { | ||
if (!_.isArray(transmission[listName])) { | ||
return transmission.recipients; | ||
} | ||
|
||
return transmission.recipients.concat(_.map(transmission[listName], (recipient) => { | ||
recipient.address = addressToObject(recipient.address); | ||
|
||
recipient.address.header_to = headerTo; | ||
|
||
// remove name from address - name is only put in the header for cc and not at all for bcc | ||
if (_.has(recipient.address, 'name')) { | ||
delete recipient.address.name; | ||
} | ||
|
||
return recipient; | ||
})); | ||
} | ||
|
||
function generateCCHeader(transmission) { | ||
return _.map(transmission.cc, (ccRecipient) => addressToString(ccRecipient.address)).join(', '); | ||
} | ||
|
||
function generateHeaderTo(recipients) { | ||
// if a recipient has a header_to then it is cc'd or bcc'd and we don't want it in the header_to value | ||
const originalRecipients = _.filter(recipients, (recipient) => !_.has(recipient.address, 'header_to')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this line do? I don't see any examples where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If they do a weird mix of the full syntax and the shortcut - i.e. they set the |
||
|
||
return _.map(originalRecipients, (recipient) => addressToString(recipient.address)).join(', '); | ||
} | ||
|
||
function addressToString(address) { | ||
if (_.isPlainObject(address)) { | ||
if (_.has(address, 'name')) { | ||
address = `"${address.name}" <${address.email}>`; | ||
} else { | ||
address = address.email; | ||
} | ||
} | ||
|
||
return address; | ||
} | ||
|
||
function addressToObject(address) { | ||
let addressObject = address; | ||
|
||
if (_.isString(address)) { | ||
addressObject = {}; | ||
|
||
const matches = /"?(.[^"]*)?"?\s*<(.+)>/gi.exec(address); | ||
|
||
if (matches) { | ||
addressObject.name = matches[1]; | ||
addressObject.email = matches[2]; | ||
} else { | ||
addressObject.email = address; | ||
} | ||
} | ||
|
||
return addressObject; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need that check for
list_id
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, that check was incorrect. I just reviewed the docs.