add send mail lib

This commit is contained in:
Jay 2017-06-05 18:25:48 +08:00
parent 69d560a21a
commit e094b97f10
6 changed files with 53 additions and 1 deletions

View File

@ -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();

View File

@ -16,5 +16,13 @@
"port": 27017,
"dbname": "lora"
}
},
"smtp": {
"sys_mail": "admin@localhost",
"host": "localhost",
"port": 587,
"secure": false,
"user": "admin",
"pass": "pass"
}
}

View File

@ -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: '<b>Hello world ?</b>' // 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);
});
});
}

View File

@ -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"
}
}

View File

@ -159,6 +159,9 @@ router
c.serr = err;
throw 'SE0005';
}
c.body = {
record: []
}
})

View File

@ -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"