From ea4f40b628e5c47b1fa64e2fb0b90fa8a97dd6a0 Mon Sep 17 00:00:00 2001 From: Hirunatan Date: Tue, 17 Jul 2018 12:05:27 +0200 Subject: [PATCH] Implement SMTP email service (#28) --- config/default.json | 19 ++++++++++++++---- helpers/mailer.js | 48 +++++++++++++++++++++++++++++++++++++-------- package.json | 1 + routes/api/users.js | 2 +- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/config/default.json b/config/default.json index 37a00aa..04ff126 100644 --- a/config/default.json +++ b/config/default.json @@ -1,8 +1,10 @@ { - //"endpoint": "http://localhost:9000", - "endpoint": "http://localhost:9666", - "storage_region": "eu-central-1", + "team_name": "My Open Spacedeck", + "contact_email": "support@example.org", + "endpoint": "http://localhost:9666", + + "storage_region": "eu-central-1", //"storage_bucket": "sdeck-development", //"storage_cdn": "http://localhost:9123/sdeck-development", //"storage_endpoint": "http://storage:9000", @@ -18,5 +20,14 @@ "google_access" : "", "google_secret" : "", "admin_pass": "very_secret_admin_password", - "phantom_api_secret": "very_secret_phantom_password" + "phantom_api_secret": "very_secret_phantom_password", + + // Choose "console" or "smtp" + "mail_provider": "smtp", + "mail_smtp_host": "your.smtp.host", + "mail_smtp_port": 465, + "mail_smtp_secure": true, + "mail_smtp_require_tls": true, + "mail_smtp_user": "your.smtp.user", + "mail_smtp_pass": "your.secret.smtp.password" } diff --git a/helpers/mailer.js b/helpers/mailer.js index 835dfb9..39a0a3c 100644 --- a/helpers/mailer.js +++ b/helpers/mailer.js @@ -1,18 +1,18 @@ 'use strict'; -var swig = require('swig'); +const config = require('config'); +const nodemailer = require('nodemailer'); +const swig = require('swig'); //var AWS = require('aws-sdk'); module.exports = { sendMail: (to_email, subject, body, options) => { - if (!options) { options = {}; } - // FIXME - const teamname = options.teamname || "My Open Spacedeck" - const from = teamname + ' '; + const teamname = options.teamname || config.get('team_name'); + const from = teamname + ' <' + config.get('contact_email') + '>'; let reply_to = [from]; if (options.reply_to) { @@ -29,9 +29,40 @@ module.exports = { options: options }); - //if (process.env.NODE_ENV === 'development') { + if (config.get('mail_provider') === 'console') { + console.log("Email: to " + to_email + " in production.\nreply_to: " + reply_to + "\nsubject: " + subject + "\nbody: \n" + htmlText + "\n\n plaintext:\n" + plaintext); - /*} else { + + } else if (config.get('mail_provider') === 'smtp') { + + const transporter = nodemailer.createTransport({ + host: config.get('mail_smtp_host'), + port: config.get('mail_smtp_port'), + secure: config.get('mail_smtp_secure'), + requireTLS: config.get('mail_smtp_require_tls'), + auth: { + user: config.get('mail_smtp_user'), + pass: config.get('mail_smtp_pass'), + } + }); + + transporter.sendMail({ + from: from, + replyTo: reply_to, + to: to_email, + subject: subject, + text: plaintext, + html: htmlText, + }, function(err, info) { + if (err) { + console.error("Error sending email:", err); + } else { + console.log("Email sent."); + } + }); + + } else if (config.get('mail_provider') === 'aws') { + /* AWS.config.update({region: 'eu-west-1'}); var ses = new AWS.SES(); @@ -56,6 +87,7 @@ module.exports = { if (err) console.error("Error sending email:", err); else console.log("Email sent."); }); - }*/ + */ + } } }; diff --git a/package.json b/package.json index 2dae5c8..e5be5e5 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "moment": "^2.19.3", "morgan": "1.8.1", "node-phantom-simple": "2.2.4", + "nodemailer": "^4.6.7", "phantomjs-prebuilt": "2.1.14", "read-chunk": "^2.1.0", "request": "2.81.0", diff --git a/routes/api/users.js b/routes/api/users.js index 3fd1786..2898beb 100644 --- a/routes/api/users.js +++ b/routes/api/users.js @@ -283,7 +283,7 @@ router.post('/password_reset_requests', (req, res, next) => { router.post('/password_reset_requests/:confirm_token/confirm', function(req, res, next) { var password = req.body.password; - User + db.User .findOne({where: {"password_reset_token": req.params.confirm_token}}) .then((user) => { if (user) {