Fix the mailer

This commit is contained in:
Evert Prants 2020-06-01 20:14:11 +03:00
parent 787cc6fc73
commit 2a7aec1b46
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
1 changed files with 23 additions and 36 deletions

View File

@ -1,68 +1,55 @@
import { EmailTemplate } from 'email-templates' import Email from 'email-templates'
import path from 'path' import path from 'path'
import nodemailer from 'nodemailer' import nodemailer from 'nodemailer'
import config from '../../scripts/load-config' import config from '../../scripts/load-config'
// TEMPORARY FIX FOR NODE v9.2.0
import tls from 'tls'
tls.DEFAULT_ECDH_CURVE = 'auto'
const templateDir = path.join(__dirname, '../../', 'templates') const templateDir = path.join(__dirname, '../../', 'templates')
const email = new Email({
const templateCache = {} message: {
let transporter from: config.email.admin
},
views: {
root: path.resolve(templateDir)
},
send: true
})
// Send an email to `email` with `headers` // Send an email to `email` with `headers`
async function sendMail (email, headers) { async function sendMail (address, template, context) {
return new Promise(function (resolve, reject) { if (!email.transport) throw new Error('No transporter present!')
if (!transporter) return reject(new Error('No transporter present!'))
transporter.sendMail(Object.assign({ return email.send({
from: config.email.admin, template,
to: email locals: context,
}, headers), (error, info) => { message: {
if (error) { to: address
return reject(error) }
}
resolve(info)
})
}) })
} }
// Send an email to `email` using `template` rendered with variables from `context` // Send an email to `email` using `template` rendered with variables from `context`
async function pushMail (template, email, context) { async function pushMail (template, address, context) {
if (!transporter) return null
let templ = null
if (!templateCache[template]) {
templ = templateCache[template] = new EmailTemplate(path.join(templateDir, template))
} else {
templ = templateCache[template]
}
const result = await templ.render(context)
console.debug('Mail being sent: %s to %s', template, email) console.debug('Mail being sent: %s to %s', template, email)
return sendMail(email, result) return sendMail(address, template, context)
} }
// Transporter initialization // Transporter initialization
async function init () { async function init () {
if (!config.email || config.email.enabled === false) return if (!config.email || config.email.enabled === false) return
transporter = nodemailer.createTransport(config.email.transport) const transporter = nodemailer.createTransport(config.email.transport)
console.debug('Setting up mail transporter') console.debug('Setting up mail transporter')
try { try {
await transporter.verify() await transporter.verify()
email.config.transport = transporter
email.transport = transporter
console.debug('Mail transporter initialized') console.debug('Mail transporter initialized')
} catch (e) { } catch (e) {
console.error('Email server verification failed') console.error('Email server verification failed')
console.error(e) console.error(e)
transporter = null
} }
} }