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

36 lines
1.1 KiB
JavaScript

const nodemailer = require('nodemailer')
const config = require('../config.json')
const util = require('util')
module.exports = async(toMail, template = {}, 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: 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
}
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);
return resolve(info)
})
})
}