Created
May 26, 2015 10:00
-
-
Save martyndavies/baa66f6b78602c3f211a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sendgrid = require('sendgrid')(api_key); | |
var moment = require('moment'); // I use this for time cos it's ace | |
var userInfo = require('./test.json'); // for example purposes this would be the JSON you sent me, you would probably be getting this from a DB or via another call... | |
var email = new sendgrid.Email({to: userInfo.emailData.email}); // Set up the initial email object, we're sending to Andy. | |
var timeToSend = moment(userInfo.send_time).unix(); // Scheduled send takes a UNIX timestamp, I used MomentJS to make it | |
email.setSendAt(timeToSend); // Set the scheduled send time | |
email.setUniqueArgs({user: userInfo.User}); // Let's pass the username via a unique arg, you'll get this back via the Event Webhook, helps to uniquify tracking | |
email.addCategory('daily update', 'personalised'); // Additionally, any category you want, this is also kind of like tagging, you'll get this back via Event Webhook, it'll also show in aggregated stats in the SG dashboard | |
email.subject = "This is a subject"; // Whatever words you want | |
email.addFilter('templates', 'enable', 1); // Turn on templates for this email | |
email.addFilter('templates', 'template_id', userInfo.template); // Which template to use | |
// Set up a loop to do the stories build | |
for(var i; i < userInfo.Data.length; i++) { | |
email.addSubstitution('subheading', userInfo.Data[i].subheading); | |
for(var s; s < userInfo.Data[i].stories.length; s++){ | |
email.addSubstitution('url', userInfo.Data[i].stories[s].URL); | |
email.addSubstitution('headline', userInfo.Data[i].stories[s].Headline); | |
email.addSubstitution('short_story', userInfo.Data[i].stories[s].ShortStory); | |
} | |
} | |
sendgrid.send(email, function(err, json) { console.log(err | json); }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment