From 9b291889ac2fa6b540d9f3f1e54217ba284d5177 Mon Sep 17 00:00:00 2001 From: Ben Ramey Date: Mon, 14 Apr 2014 17:25:57 -0500 Subject: [PATCH] Add email service, refactor Run model to use it --- api/models/Run.js | 37 +----------------------------------- api/services/EmailService.js | 32 +++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 34 insertions(+), 36 deletions(-) create mode 100644 api/services/EmailService.js diff --git a/api/models/Run.js b/api/models/Run.js index cdaf745..728f5da 100644 --- a/api/models/Run.js +++ b/api/models/Run.js @@ -5,8 +5,6 @@ * @description :: A short summary of how this model works and what it represents. * @docs :: http://sailsjs.org/#!documentation/models */ -var nodemailer = require('nodemailer'); - module.exports = { attributes: { @@ -32,40 +30,7 @@ module.exports = { }, afterCreate: function(values, next) { - var transport = nodemailer.createTransport('SMTP', { - service: "Gmail", - auth: { - user: "ben.ramey@gmail.com", - pass: "dxir9PwxRwh9tauwcEbj" - } - }); - - var emailText = "You've been invited to " + values.creatorName + "'s lunch run: '" - + values.name + "'! \n\n Put in your order here:\nhttp://www.lunchrun.me/#/run/" - + values.id + "/order/"; - var options = { - from: values.creatorName + " <" + values.creatorEmail + ">", - subject: "You've been invited to a lunch run!" - }; - - for (var idx in values.invitees) { - var invitee = values.invitees[idx]; - var specificOptions = options; - specificOptions.to = invitee.name + " <" + invitee.email + ">"; - specificOptions.text = emailText + invitee.name; - - console.log(specificOptions); - transport.sendMail(specificOptions, function(err, response) { - if (err) { - console.log(err); - } else { - console.log("message sent from: " + values.creatorEmail); - } - }); - } - - transport.close(); - + EmailService.sendRunEmails(values); next(); }, }; diff --git a/api/services/EmailService.js b/api/services/EmailService.js new file mode 100644 index 0000000..9890894 --- /dev/null +++ b/api/services/EmailService.js @@ -0,0 +1,32 @@ +// EmailService.js +var sendgrid = require("sendgrid"); + +exports.sendRunEmails = function(runData) { + var emailText = "You've been invited to " + runData.creatorName + "'s lunch run: '" + + runData.name + "'! \n\n Put in your order here:\nhttp://www.lunchrun.me/#/run/" + + runData.id + "/order/"; + var options = { + from: runData.creatorName + " <" + runData.creatorEmail + ">", + subject: "You've been invited to a lunch run!", + replyto: runData.creatorEmail + }; + + for (var idx in runData.invitees) { + var invitee = runData.invitees[idx]; + var specificOptions = options; + specificOptions.to = invitee.name + " <" + invitee.email + ">"; + specificOptions.text = emailText + invitee.name; + + console.log(specificOptions); + + sendgrid.send(specificOptions, function(err, response) { + if (err) { + console.log(err); + } else { + console.log("message sent from: " + runData.creatorEmail); + } + }); + } + + transport.close(); +}; diff --git a/package.json b/package.json index 45a1539..34f83a6 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "nodemailer": "~0.6.1", "load-grunt-tasks": "~0.3.0", "glob": "3.2.8", + "sendgrid": "1.0.2", "grunt-contrib-clean": "~0.5.0", "grunt-contrib-copy": "~0.5.0", "grunt-contrib-concat": "~0.3.0",