lora-project/server-api/libs/sendmail.js

35 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-06-05 10:25:48 +00:00
const nodemailer = require('nodemailer');
const config = require('../config.json');
const util = require('util');
2017-06-05 10:25:48 +00:00
module.exports = async(toMail, template = {}, data = []) => {
2017-06-05 10:25:48 +00:00
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: template.title || '', // Subject line
text: template.text ? util.format(template.text, ...data) : '', // plain text body
html: template.html ? util.format(template.html, ...data) : '' // html body
2017-06-05 10:25:48 +00:00
};
return new Promise((resolve, reject) => {
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return reject(error);
}
// console.log('Mepassage %s sent: %s', info.messageId, info.response);
2017-06-05 10:25:48 +00:00
return resolve(info);
});
});
}