Skip to content

Instantly share code, notes, and snippets.

@jamielob
Last active December 15, 2016 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamielob/c5cc6353203effcac8b03014b684016d to your computer and use it in GitHub Desktop.
Save jamielob/c5cc6353203effcac8b03014b684016d to your computer and use it in GitHub Desktop.
import { Meteor } from 'meteor/meteor';
import Sendgrid from 'sendgrid';
import * as helper from 'sendgrid/lib/helpers/mail/mail';
import _ from 'lodash';
// Article
// https://medium.com/@jamielob/sending-branded-emails-from-node-js-66ffa9c5a6c6#.kddhj4go6
export const sendTemplateEmail = ({
templateId,
subject,
to,
substitutions,
}) => {
const {
SENDGRID_KEY,
} = Meteor.settings;
const mailer = Sendgrid(SENDGRID_KEY);
const email = new helper.Mail();
const fromEmail = new helper.Email('info@flyblackbird.com');
email.setFrom(fromEmail);
email.setSubject(subject);
const content = new helper.Content('text/html', 'Template');
email.addContent(content);
const personalization = new helper.Personalization();
const toEmail = new helper.Email(to);
personalization.addTo(toEmail);
_.each(substitutions, ([variable, value]) => {
const sub = new helper.Substitution(variable, value);
personalization.addSubstitution(sub);
});
email.addPersonalization(personalization);
email.setTemplateId(templateId);
const request = mailer.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: email.toJSON(),
});
mailer.API(request, (e, response) => {
if (e) {
throw new Error(e.message);
}
return response;
});
};
// sendTemplateEmail({
// templateId: '88f55776-d556-42cd-a668-dd55c925e342',
// subject: 'Welcome!',
// to: 'jamie@flyblackbird.com',
// substitutions: [
// ['%firstName%', 'Jamie'],
// ],
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment