From 1aadb08db0c764b186cc469c22c282a0ef8d5da0 Mon Sep 17 00:00:00 2001 From: jkaplon Date: Sat, 27 Jun 2015 16:43:53 -0400 Subject: [PATCH] Add basic email functionality w/nodemailer. --- package.json | 3 ++- server.js | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2a9a907..a6c6dea 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/server.js b/server.js index 13de886..2192e4c 100644 --- a/server.js +++ b/server.js @@ -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 ', + 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)); });