loopback-connector-sparkpost
is the Loopback connector module which allow to send emails via Sparkpost.
npm install loopback-connector-sparkpost --save
datasources.json
{
"sparkpost": {
"connector": "loopback-connector-sparkpost",
"apiKey": "[your api key here]",
"defaults": {
"options": {
"start_time": "now"
}
}
}
}
model-config.json
{
"Email": {
"dataSource": "sparkpost",
"public": false
}
}
Additionaly you can set defaults
{
"sparkpost": {
"connector": "loopback-connector-sparkpost",
"apiKey": "[your api key here]",
"defaults": {
"content": {
"from": { name: "Bob Schmoe", email: "[email protected]" }
}
}
}
}
Configuration in JavaScript
var DataSource = require('loopback-datasource-juggler').DataSource;
var dsSparkpost = new DataSource('loopback-connector-sparkpost', {
apiKey: '[your api key here]'
});
loopback.Email.attachTo(dsSparkpost);
Basic option same as built in Loopback
loopback.Email.send({
to: "[email protected]",
from: "[email protected]",
subject: "subject",
text: "text message",
html: "html <b>message</b>"
},
function(err, result) {
if(err) {
console.log('Upppss something crash');
return;
}
console.log(result);
});
Some advantages - now you can use templates from Sparkpost
loopback.Email.send({
to: "[email protected]",
from: "[email protected]",
subject: "subject",
template: {
id: "signup-confirm",
content: {
name: "NewUser Name",
accountId: "123456"
}
}
},
function(err, result) {
if(err) {
console.log('Upppss something crash');
return;
}
console.log(result);
});
Be sure to test from one of the approved domains.
apiKey=hwgfhfhjgrfhjekjheiff [email protected] startTime=2016-09-30T17:55:11 mocha basic.test.js --timeout 15000