From e094b97f10fe721b2b9fe24407f9f95b4195fce4 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 5 Jun 2017 18:25:48 +0800 Subject: [PATCH] add send mail lib --- server-api/app.js | 2 ++ server-api/config.json.default | 8 ++++++++ server-api/libs/sendmail.js | 34 +++++++++++++++++++++++++++++++++ server-api/package.json | 3 ++- server-api/route/api/account.js | 3 +++ server-api/yarn.lock | 4 ++++ 6 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 server-api/libs/sendmail.js diff --git a/server-api/app.js b/server-api/app.js index eadb8d0..7799b94 100644 --- a/server-api/app.js +++ b/server-api/app.js @@ -9,7 +9,9 @@ const config = require('./config.json'); const mysql = require('./libs/mysql_pool'); const so = require('./libs/storeObject'); const mem = require('./libs/memcache_lib'); +const sendmail = require('./libs/sendmail'); +// init memcached connection mem.host = 'dyn.trj.tw'; mem.port = 24002; mem.connect(); diff --git a/server-api/config.json.default b/server-api/config.json.default index a3a5f5e..f0dc031 100644 --- a/server-api/config.json.default +++ b/server-api/config.json.default @@ -16,5 +16,13 @@ "port": 27017, "dbname": "lora" } + }, + "smtp": { + "sys_mail": "admin@localhost", + "host": "localhost", + "port": 587, + "secure": false, + "user": "admin", + "pass": "pass" } } \ No newline at end of file diff --git a/server-api/libs/sendmail.js b/server-api/libs/sendmail.js new file mode 100644 index 0000000..848a539 --- /dev/null +++ b/server-api/libs/sendmail.js @@ -0,0 +1,34 @@ +const nodemailer = require('nodemailer'); +const config = require('../config.json'); + +module.exports = async(toMail, type = 'forgotpass', data = []) => { + let transporter = nodemailer.createTransport({ + host: config.smtp.host, + port: config.smtp.port, + secure: config.smtp.secure, // secure:true for port 465, secure:false for port 587 + auth: { + user: config.smtp.user, + pass: config.smtp.pass + } + }); + + // setup email data with unicode symbols + let mailOptions = { + from: config.smtp.sys_mail, // sender address + to: toMail, // list of receivers + subject: 'Hello ✔', // Subject line + text: 'Hello world ?', // plain text body + html: 'Hello world ?' // html body + }; + + return new Promise((resolve, reject) => { + // send mail with defined transport object + transporter.sendMail(mailOptions, (error, info) => { + if (error) { + return reject(error); + } + // console.log('Message %s sent: %s', info.messageId, info.response); + return resolve(info); + }); + }); +} \ No newline at end of file diff --git a/server-api/package.json b/server-api/package.json index 8075ff7..615e8f3 100644 --- a/server-api/package.json +++ b/server-api/package.json @@ -14,6 +14,7 @@ "koa-static": "^3.0.0", "memcached": "^2.2.2", "mongoose": "^4.10.4", - "mysql": "^2.13.0" + "mysql": "^2.13.0", + "nodemailer": "^4.0.1" } } diff --git a/server-api/route/api/account.js b/server-api/route/api/account.js index 2139d14..d6c500f 100644 --- a/server-api/route/api/account.js +++ b/server-api/route/api/account.js @@ -159,6 +159,9 @@ router c.serr = err; throw 'SE0005'; } + c.body = { + record: [] + } }) diff --git a/server-api/yarn.lock b/server-api/yarn.lock index 9d85d65..86d37e1 100644 --- a/server-api/yarn.lock +++ b/server-api/yarn.lock @@ -415,6 +415,10 @@ negotiator@0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9" +nodemailer@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-4.0.1.tgz#b95864b07facee8287e8232effd6f1d56ec75ab2" + object-assign@^4.0.1: version "4.1.1" resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"