Add basic email functionality w/nodemailer.

This commit is contained in:
jkaplon 2015-06-27 16:43:53 -04:00
parent 92bba362ad
commit 1aadb08db0
2 changed files with 26 additions and 2 deletions

View File

@ -5,7 +5,8 @@
"body-parser": "^1.12.4",
"express": "^4.12.4",
"sqlite3": "^3.0.8",
"express-hbs": "^0.8.4"
"express-hbs": "^0.8.4",
"nodemailer": "^1.3.4"
},
"scripts": { "start": "node server.js" }
}

View File

@ -1,10 +1,26 @@
var express = require("express");
var hbs = require('express-hbs');
var nodemailer = require('nodemailer');
var fs = require("fs");
var bodyParser = require("body-parser");
var app = express();
var logfile = fs.createWriteStream('./db/log.log', {flags: 'a'});
// Setup email
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'jkU8K5lrog9w@gmail.com',
pass: 'NSLQZ~d6Zb~LLv8#t'
}
});
var mailOptions = {
from: 'Alert Monitor <jkU8K5lrog9w@gmail.com>',
to: 'jody@kaplon.us',
subject: 'Alert received',
text: 'test alert'
};
var file = "./db/test.db";
var exists = fs.existsSync(file);
@ -42,7 +58,7 @@ app.get('/', function(req, res){
//res.sendFile("/usr/src/app/index.html");
//fs.createReadStream('./log.log').pipe(res);
console.log("got a GET request...");
db.all("SELECT coreId, published_at FROM Alerts", function(err, rows){
db.all("SELECT coreId, published_at FROM Alerts ORDER BY published_at DESC LIMIT 30;", function(err, rows){
console.log(rows);
res.render('home', {alerts: rows}, function(err, html) {
res.send(html);
@ -64,6 +80,13 @@ app.post('/', function(req, res){
JSON.stringify(req.body.published_at, null, 4)
);
stmt.finalize();
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' + info.response);
}
});
//res.send(JSON.stringify(req.body, null, 4));
});