From f171bf2851e88d516fe84dd18d62261d62975469 Mon Sep 17 00:00:00 2001 From: Chuck Williams Date: Thu, 9 Feb 2017 00:57:58 -0700 Subject: [PATCH 1/2] WIP: Stubbing email send route --- .gitignore | 3 ++- AppConfig.js | 2 +- Server.js | 12 ++++++++++++ package.json | 2 ++ service/EmailService.js | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 service/EmailService.js diff --git a/.gitignore b/.gitignore index 49074af..f4cddf4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Dependency directory # Commenting this out is preferred by some people, see # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- -node_modules \ No newline at end of file +node_modules +yarn.lock \ No newline at end of file diff --git a/AppConfig.js b/AppConfig.js index e6c3bbf..c21f7ce 100644 --- a/AppConfig.js +++ b/AppConfig.js @@ -43,7 +43,7 @@ var config = { app: { name: 'dash.org-api' }, - port: process.env.PORT || 9090, + port: process.env.PORT || 8080, logLevel: 'DEBUG' // EMERGENCY|ALERT|CRITICAL|ERROR|WARNING|NOTICE|INFO|DEBUG }, diff --git a/Server.js b/Server.js index 940fe1d..239c124 100644 --- a/Server.js +++ b/Server.js @@ -7,6 +7,12 @@ var AppConfig = require('./AppConfig'); var Cache = require('./service/CacheRepository'); var Masternode = require('./service/MasternodeService'); var Blockchain = require('./service/BlockchainService'); +var bodyParser = require('body-parser') + +app.use( bodyParser.json() ); // to support JSON-encoded bodies +app.use(bodyParser.urlencoded({ // to support URL-encoded bodies + extended: true +})); // Add headers app.use(function (req, res, next) { @@ -87,6 +93,12 @@ app.get('/exchange', function(req,res){ }); +app.post('/send-contact-email', function(req,res){ + + res.send(req.body); + +}); + global.cacheConnection = Cache.connect(); var server = app.listen(AppConfig.port, function () { diff --git a/package.json b/package.json index c25529a..9a83bde 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,11 @@ "dependencies": { "async": "^2.0.0", "aws-sdk": "^2.4.7", + "body-parser": "^1.16.0", "express": "^4.13.3", "log": "^1.4.0", "memcached": "^2.2.2", + "nodemailer": "^3.0.2", "request": "^2.60.0" } } diff --git a/service/EmailService.js b/service/EmailService.js new file mode 100644 index 0000000..cd36f25 --- /dev/null +++ b/service/EmailService.js @@ -0,0 +1,33 @@ +/** + * Created by cwilliams on 2/9/17. + */ + +var smtpTransport = nodemailer.createTransport("SMTP",{ + service: "Gmail", + auth: { + user: "", + pass: "" + } +}); + +function sendMail(to, subject, text) +var mailOptions={ + to : req.query.to, + subject : req.query.subject, + text : req.query.text +} +console.log(mailOptions); +smtpTransport.sendMail(mailOptions, function(error, response){ + if(error){ + console.log(error); + res.end("error"); + }else{ + console.log("Message sent: " + response.message); + res.end("sent"); + } +}); + + +module.exports = { + +} \ No newline at end of file From f62f445d50f9b4d2c68b0dfddf1aa5a805ad1dd2 Mon Sep 17 00:00:00 2001 From: Chuck Williams Date: Sat, 11 Feb 2017 01:29:11 -0700 Subject: [PATCH 2/2] adds draft functional email send api "/send-contact-email" (depends on nodemailer) --- AppConfig.js | 140 +++++++++++++++++++++++----------------- Server.js | 4 +- service/EmailService.js | 48 +++++++------- 3 files changed, 108 insertions(+), 84 deletions(-) diff --git a/AppConfig.js b/AppConfig.js index c21f7ce..2ebec10 100644 --- a/AppConfig.js +++ b/AppConfig.js @@ -1,67 +1,89 @@ -var path = require('path'); +var path = require('path'); var rootPath = path.normalize(__dirname + '/..'); -var env = process.env.NODE_ENV || 'development'; +var env = process.env.NODE_ENV || 'development'; var config = { - development: { - exchanges: { - coincap: { - name: 'CoinCap', - orgUrl: 'http://coincap.io/', - url: 'https://api.coinmarketcap.com/v1/ticker/dash' - }, - worldcoin: { - name: 'WorldCoinIndex', - orgUrl: 'https://www.worldcoinindex.com', - url: 'https://www.worldcoinindex.com/apiservice/json', - apiKey: 'ePSl8tl8dsFhLyReZ6aIwCQNw' - } - }, - budgets: { - dashwhale: { - name: 'DashWhale', - url: 'https://www.dashwhale.org/api/v1/budget' - } - }, - masternodes: { - node40: { - name: 'Node40', - apiKey: '8e4ec80a6394db3411b8af9afa3b5156d36fd18102f1278fef59a3e40d9bf6af', - apiKeyHeader: 'X-Api-Key', - historyUrl: 'https://node40.com/monitor/api/masterNode/history', - statsUrl: 'https://node40.com/monitor/api/masterNode/stats' - } - }, - chain: { - insight: { - name: 'Insight', - url: 'https://insight.dash.siampm.com/api/blocks', - maxBlocks: 10 - } - }, - root: rootPath, - app: { - name: 'dash.org-api' - }, - port: process.env.PORT || 8080, - logLevel: 'DEBUG' // EMERGENCY|ALERT|CRITICAL|ERROR|WARNING|NOTICE|INFO|DEBUG - }, + development: { + exchanges: { + coincap: { + name: 'CoinCap', + orgUrl: 'http://coincap.io/', + url: 'https://api.coinmarketcap.com/v1/ticker/dash' + }, + worldcoin: { + name: 'WorldCoinIndex', + orgUrl: 'https://www.worldcoinindex.com', + url: 'https://www.worldcoinindex.com/apiservice/json', + apiKey: 'ePSl8tl8dsFhLyReZ6aIwCQNw' + } + }, + budgets: { + dashwhale: { + name: 'DashWhale', + url: 'https://www.dashwhale.org/api/v1/budget' + } + }, + masternodes: { + node40: { + name: 'Node40', + apiKey: '8e4ec80a6394db3411b8af9afa3b5156d36fd18102f1278fef59a3e40d9bf6af', + apiKeyHeader: 'X-Api-Key', + historyUrl: 'https://node40.com/monitor/api/masterNode/history', + statsUrl: 'https://node40.com/monitor/api/masterNode/stats' + } + }, + chain: { + insight: { + name: 'Insight', + url: 'https://insight.dash.siampm.com/api/blocks', + maxBlocks: 10 + } + }, + root: rootPath, + app: { + name: 'dash.org-api' + }, + email: { + contactEmailAddress: "contact@dash.org", + transportConfig : { + port: 2525, + host: "localhost", + secure: false, + auth: { + user: "localWebUser", + pass: "asdfg" + } + } + }, + port: process.env.PORT || 8080, + logLevel: 'DEBUG' // EMERGENCY|ALERT|CRITICAL|ERROR|WARNING|NOTICE|INFO|DEBUG + }, - test: { - root: rootPath, - app: { - name: 'dash.org-api' - }, - port: process.env.PORT || 9090 - }, + test: { + root: rootPath, + app: { + name: 'dash.org-api' + }, + port: process.env.PORT || 9090 + }, - production: { - root: rootPath, - app: { - name: 'dash.org-api' - }, - port: process.env.PORT || 9090 - } + production: { + root: rootPath, + app: { + name: 'dash.org-api' + }, + port: process.env.PORT || 9090, + email: { + contactEmailAddress: "contact@dash.org", + transportConfig : { + service: "Gmail", + auth: { + user: "", + pass: "" + } + } + }, + } }; module.exports = config[env]; diff --git a/Server.js b/Server.js index 239c124..85e4761 100644 --- a/Server.js +++ b/Server.js @@ -7,6 +7,7 @@ var AppConfig = require('./AppConfig'); var Cache = require('./service/CacheRepository'); var Masternode = require('./service/MasternodeService'); var Blockchain = require('./service/BlockchainService'); +var Email = require('./service/EmailService'); var bodyParser = require('body-parser') app.use( bodyParser.json() ); // to support JSON-encoded bodies @@ -95,7 +96,8 @@ app.get('/exchange', function(req,res){ app.post('/send-contact-email', function(req,res){ - res.send(req.body); + var results = Email.sendMail(AppConfig.email.contactEmailAddress,req.body.subject, req.body.message); + res.send(results); }); diff --git a/service/EmailService.js b/service/EmailService.js index cd36f25..250f88c 100644 --- a/service/EmailService.js +++ b/service/EmailService.js @@ -1,33 +1,33 @@ /** * Created by cwilliams on 2/9/17. */ +var nodemailer = require("nodemailer"); +var AppConfig = require("../AppConfig"); -var smtpTransport = nodemailer.createTransport("SMTP",{ - service: "Gmail", - auth: { - user: "", - pass: "" - } -}); +var transport = AppConfig.email.transportConfig; +console.log('transport: ', transport); +var smtpTransport = nodemailer.createTransport(transport); -function sendMail(to, subject, text) -var mailOptions={ - to : req.query.to, - subject : req.query.subject, - text : req.query.text +function sendMail(to, subject, text) { + var mailOptions = { + to: to, + subject: subject, + text: text + }; + + console.log(mailOptions); + smtpTransport.sendMail(mailOptions, function (error, response) { + if (error) { + console.log(error); + res.end("error"); + } else { + console.log("Message sent: " + response.message); + res.end("sent"); + } + }); } -console.log(mailOptions); -smtpTransport.sendMail(mailOptions, function(error, response){ - if(error){ - console.log(error); - res.end("error"); - }else{ - console.log("Message sent: " + response.message); - res.end("sent"); - } -}); module.exports = { - -} \ No newline at end of file + sendMail: sendMail +}; \ No newline at end of file