import Email from 'email-templates' import path from 'path' import nodemailer from 'nodemailer' import config from '../../scripts/load-config' const templateDir = path.join(__dirname, '../../', 'templates') const email = new Email({ message: { from: config.email.admin }, views: { root: path.resolve(templateDir) }, send: true }) // Send an email to `email` with `headers` async function sendMail (address, template, context) { if (!email.transport) throw new Error('No transporter present!') return email.send({ template, locals: context, message: { to: address } }) } // Send an email to `email` using `template` rendered with variables from `context` async function pushMail (template, address, context) { console.debug('Mail being sent: %s to %s', template, email) return sendMail(address, template, context) } // Transporter initialization async function init () { if (!config.email || config.email.enabled === false) return const transporter = nodemailer.createTransport(config.email.transport) console.debug('Setting up mail transporter') try { await transporter.verify() email.config.transport = transporter email.transport = transporter console.debug('Mail transporter initialized') } catch (e) { console.error('Email server verification failed') console.error(e) } } module.exports = { sendMail: sendMail, pushMail: pushMail, init: init }