Files
lunchrun.me/api/models/Run.js
Ben Ramey d1de555169 Emailing
2014-03-22 17:58:56 -05:00

73 lines
1.7 KiB
JavaScript

/**
* Run
*
* @module :: Model
* @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: {
name: {
type: 'string',
required: true
},
creatorName: {
type: 'string',
required: true
},
creatorEmail: {
type: 'email',
required: true
},
invitees: {
type: 'array'
},
menuUrl: {
type: 'url'
},
restaurant: 'string'
},
beforeCreate: function(values, next) {
var transport = nodemailer.createTransport('SMTP', {
service: "Gmail",
auth: {
user: "ben.ramey@gmail.com",
pass: "Mila(18)?"
}
});
var inviteeEmails = [];
for (var i in values.invitees) {
var invitee = values.invitees[i];
var emailStr = invitee.name + " <" + invitee.email + ">";
inviteeEmails.push(emailStr);
}
var options = {
from: values.creatorName + " <" + values.creatorEmail + ">",
to: inviteeEmails.join(","),
subject: "A lunch run!",
text: "You've been invited to a lunch run!"
};
console.log(options);
transport.sendMail(options, function(err, response) {
if (err) {
console.log(err);
} else {
console.log("message sent from: " + values.creatorEmail);
}
});
transport.close();
next();
},
};