Skip to content

Instantly share code, notes, and snippets.

@GiriVakkalanka
Created August 1, 2017 17:29
Show Gist options
  • Save GiriVakkalanka/333ae1675127fa483c383e0189212e0a to your computer and use it in GitHub Desktop.
Save GiriVakkalanka/333ae1675127fa483c383e0189212e0a to your computer and use it in GitHub Desktop.
SendGrid setup
const sendgrid = require('sendgrid');
const helper = sendgrid.mail; //don't want to use es6 destructuring b.c we want to call it helper. not mail.
const keys = require('../config/keys');
class Mailer extends helper.Mail {
//constructor doesnt need to receive a survey model obj.
//It just has to be an object that has a subject and recipients.
//the second parameter is the html string, that we'll call content
//this design lets us use this Mailer for sending more than just surveys
//this mailer obj is defined here created in the surveyRoutes to send the emails.
constructor({ subject, recipients }, content) {
super(); //first runs constructor for Mail class
//most of the setup here is dictated by sendgrid
this.sgApi = sendgrid(keys.sendGridKey);
this.from_email = new helper.Email('[email protected]');
this.subject = subject;
this.body = new helper.Content('text/html', content);
this.recipients = this.formatAddresses(recipients);
this.addContent(this.body); //Mail class function required to actually register the body.
this.addClickTracking(); //We write this
this.addRecipients(); //We write this
}
formatAddresses(recipients) {
//es6 destructuring requires extra parens if you're doing an arrow function
return recipients.map(({ email }) => {
return new helper.Email(email);
});
}
addClickTracking() {
const trackingSettings = new helper.TrackingSettings();
const clickTracking = new helper.ClickTracking(true, true);
trackingSettings.setClickTracking(clickTracking);
this.addTrackingSettings(trackingSettings);
}
addRecipients() {
const personalize = new helper.Personalization();
this.recipients.forEach(recipient => {
personalize.addTo(recipient);
});
this.addPersonalization(personalize); //addPersonalization class function;
}
async send() {
console.log('send got called');
const request = this.sgApi.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: this.toJSON()
});
const response = this.sgApi.API(request);
return response;
}
}
module.exports = Mailer;
const mongoose = require('mongoose');
const requireLogin = require('../middlewares/requireLogin');
const requireCredits = require('../middlewares/requireCredits');
const Mailer = require('../services/Mailer');
const surveyTemplate = require('../services/emailTemplates/surveyTemplate');
const Survey = mongoose.model('surveys');
module.exports = app => {
app.post('/api/surveys', requireLogin, requireCredits, (req, res) => {
//es6 restructuring. Out of req.body, I want to get all of these properties
//off the body thats added to req. The actual adding logic has to be added in.
const { title, subject, body, recipients } = req.body;
//use Survey.js model to represent a new instance of the survey being created
//stored in memory. Has not been persisted at this point.
const survey = new Survey({
// es6 syntax, instead of doing title: title
title,
subject,
body,
recipients: recipients.split(',').map(email => ({ email })),
//takes the list of email addresses, split into array, and return an obj
//with key of email and value of email address. Hs been shortened es6 style
_user: req.user.id, //this is mongo mongoose generated id
dateSent: Date.now()
});
//place to send the email
const mailer = new Mailer(survey, surveyTemplate(survey));
mailer.send();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment