Create a gist now

Instantly share code, notes, and snippets.

Embed
Example Node.js script for sending domains report
/* jshint camelcase:false */
var _ = require('lodash')
, q = require('q')
, config = require('config')
, sparkpost = require('sparkpost')
, cassWrapper = require('@sparkpost/msys-cassandra')
, db = cassWrapper.getDb(config.cassandra);
function getSendingDomains() {
return db.executeByStream('select * from sending_domains.cust_domains', []);
}
function getCustomers() {
return db.executeByStream('select * from accounts.accounts', []);
}
function processQueries(results) {
var domainResults = results[0]
, customerResults = results[1]
, domains
, customers;
domains = domainResults.rows;
customers = customerResults.rows;
// merge the customer name into the domains
_.forEach(domains, function(domain) {
var customer = _.find(customers, {customer_id: domain.customer_id});
domain.customer_name = 'NONE'; // defaults to 'NONE'
if (customer && customer.company_name) {
domain.customer_name = customer.company_name.toLowerCase();
}
delete domain.__columns;
});
return domains;
}
function sendMessage(domains) {
var deferred = q.defer()
, message = {};
// sparkpost API config
sparkpost = sparkpost({
host: config.sparkpost.host,
protocol: config.sparkpost.protocol,
port: config.sparkpost.port,
key: config.sparkpost.apikey
});
// message meta
message.campaign = 'sending-domains-report';
message.from = config.from;
message.subject = 'Sending Domains Report';
message.recipients = config.support;
message.substitutionData = {domains: _.map(domains['new'], function(domain) {
return _.pick(domain, ['customer_id', 'customer_name', 'domain', 'spf_status', 'dkim_status', 'abuse_at_status', 'pm_at_status', 'comply_status']);
})};
sparkpost.transmission.send(message, function(err, response) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(response);
}
});
return deferred.promise;
}
q.all([getSendingDomains(), getCustomers()])
.then(processQueries)
.then(sendMessage)
.then(function(result) {
console.log('Message ' + result.results.id + ' sent!', 'Rejected:', result.results.total_rejected_recipients, 'Accepted:', result.results.total_accepted_recipients);
process.exit(0);
})
.fail(function(err) {
console.log('Error:', err);
process.exit(1);
});
<h2>Sending Domains Report</h2>
{{ if empty(domains) }}
<p>No new sending domains have been created in the past 24 hours.</p>
{{ else }}
<table>
<tr>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Domain</th>
<th>SPF Status</th>
<th>DKIM Status</th>
<th>Abuse@ Status</th>
<th>Postmaster@ Status</th>
<th>Compliance Status</th>
</tr>
{{ each domains }}
<tr>
<td>{{ loop_var.customer_id }}</td>
<td>{{ loop_var.customer_name }}</td>
<td>{{ loop_var.domain }}</td>
<td>{{ loop_var.spf_status }}</td>
<td>{{ loop_var.dkim_status }}</td>
<td>{{ loop_var.abuse_at_status }}</td>
<td>{{ loop_var.pm_at_status }}</td>
<td>{{ loop_var.comply_status }}</td>
</tr>
{{ end }}
{{ end }}
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment