Created
April 5, 2014 22:13
-
-
Save jdcauley/9998734 to your computer and use it in GitHub Desktop.
gistforbraidn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* MailController.js | |
* | |
* @description :: | |
* @docs :: http://sailsjs.org/#!documentation/controllers | |
*/ | |
var async = require('async'); | |
var nodemailer = require('nodemailer'); | |
var smtpTransport = nodemailer.createTransport("SMTP",{ | |
service: "Mandrill", | |
auth: { | |
user: 'jdcauley', | |
pass: 'Y1VueIj75BRKTWVEOd7qHw' | |
} | |
}); | |
module.exports = { | |
create: function(req, res, next) { | |
console.log('new message'); | |
var events = req.param('mandrill_events'); | |
var parsed = JSON.parse(events); | |
async.each(parsed, function(item, callback){ | |
console.log(item.msg.email); | |
Group.findOneByAddress(item.msg.email).populate('members').exec(function(err, theGroup){ | |
Mail.create({ | |
to: item.msg.email, | |
from: item.msg.from_email, | |
subject: item.msg.subject, | |
html: item.msg.html, | |
text: item.msg.text, | |
raw: item.msg.raw_msg, | |
group: theGroup.id | |
}).done(function(err, mail){ | |
console.log('Mail Created in DB'); | |
if (err){ | |
res.send(500, err); | |
}else{ | |
res.send(200); | |
var members = theGroup.members; | |
console.log(members.count); | |
console.log(theGroup.groupname); | |
console.log('Braidns thingy' + groupEmails(theGroup.members)); | |
async.each(theGroup.members, function(member, callback){ | |
console.log('inside Group Async' + theGroup.address); | |
console.log('-----------'); | |
console.log(member.email); | |
console.log(mail.subject); | |
}); // Closes async | |
var emailCollections = []; | |
for(var i = 0; i < members.length(); i++){ | |
emailCollection.push(members[i].email); | |
} | |
var mailOptions = { | |
from: theGroup.address, | |
to: emailCollections.join(', '), | |
subject: mail.subject, | |
html: mail.html | |
} | |
smtpTransport.sendMail(mailOptions, function(err, response){ | |
if(err){ | |
console.log(err); | |
}else{ | |
console.log("Message sent: " + response.message); | |
} | |
}); // Closes Transport | |
} // Success res.send(200) | |
}); // Group Done | |
}); | |
}, | |
/* | |
groupEmails: function(members) { | |
emails = []; | |
_each(members, function(member) { | |
emails.push(member.email) | |
}); | |
return emails.joins(', '); | |
} | |
*/ | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment