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

36 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-06-06 12:40:51 +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-06 12:40:51 +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
}
})
2017-06-05 10:25:48 +00:00
// setup email data with unicode symbols
2017-06-06 12:40:51 +00:00
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
2017-06-06 12:40:51 +00:00
return new Promise((resolve, reject) => {
2017-06-05 10:25:48 +00:00
// send mail with defined transport object
2017-06-06 12:40:51 +00:00
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return reject(error)
}
// console.log('Mepassage %s sent: %s', info.messageId, info.response);
2017-06-06 12:40:51 +00:00
return resolve(info)
})
})
}