Add email service, refactor Run model to use it

This commit is contained in:
Ben Ramey
2014-04-14 17:25:57 -05:00
parent b247bdd406
commit 9b291889ac
3 changed files with 34 additions and 36 deletions

View File

@@ -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();
},
};

View File

@@ -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();
};