Updating config management for SMTP as well as finalising the work around generating and sending emails.
This commit is contained in:
parent
f6e25c3792
commit
0a733b7c7a
|
@ -5,6 +5,8 @@ const {
|
||||||
getConfigParams,
|
getConfigParams,
|
||||||
determineScopedConfig,
|
determineScopedConfig,
|
||||||
} = require("@budibase/auth").db
|
} = require("@budibase/auth").db
|
||||||
|
const { Configs } = require("../../../constants")
|
||||||
|
const email = require("../../../utilities/email")
|
||||||
|
|
||||||
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
||||||
|
|
||||||
|
@ -22,6 +24,13 @@ exports.save = async function(ctx) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// verify the configuration
|
||||||
|
switch (type) {
|
||||||
|
case Configs.SMTP:
|
||||||
|
await email.verifyConfig(configDoc)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await db.post(configDoc)
|
const response = await db.post(configDoc)
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
const CouchDB = require("../../../db")
|
const CouchDB = require("../../../db")
|
||||||
const { StaticDatabases, determineScopedConfig } = require("@budibase/auth").db
|
const { StaticDatabases, determineScopedConfig } = require("@budibase/auth").db
|
||||||
const { EmailTemplatePurpose, TemplateTypes, Configs } = require("../../../constants")
|
const {
|
||||||
|
EmailTemplatePurpose,
|
||||||
|
TemplateTypes,
|
||||||
|
Configs,
|
||||||
|
} = require("../../../constants")
|
||||||
const { getTemplateByPurpose } = require("../../../constants/templates")
|
const { getTemplateByPurpose } = require("../../../constants/templates")
|
||||||
const { getSettingsTemplateContext } = require("../../../utilities/templates")
|
const { getSettingsTemplateContext } = require("../../../utilities/templates")
|
||||||
const { processString } = require("@budibase/string-templates")
|
const { processString } = require("@budibase/string-templates")
|
||||||
const nodemailer = require("nodemailer")
|
const { createSMTPTransport } = require("../../../utilities/email")
|
||||||
|
|
||||||
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
||||||
const TYPE = TemplateTypes.EMAIL
|
const TYPE = TemplateTypes.EMAIL
|
||||||
|
@ -14,15 +18,7 @@ const FULL_EMAIL_PURPOSES = [
|
||||||
EmailTemplatePurpose.PASSWORD_RECOVERY,
|
EmailTemplatePurpose.PASSWORD_RECOVERY,
|
||||||
]
|
]
|
||||||
|
|
||||||
function createSMTPTransport(config) {
|
async function buildEmail(purpose, email, user) {
|
||||||
const transport = nodemailer.createTransport({
|
|
||||||
port: config.port,
|
|
||||||
host: config.host,
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.buildEmail = async (email, user, purpose) => {
|
|
||||||
// this isn't a full email
|
// this isn't a full email
|
||||||
if (FULL_EMAIL_PURPOSES.indexOf(purpose) === -1) {
|
if (FULL_EMAIL_PURPOSES.indexOf(purpose) === -1) {
|
||||||
throw `Unable to build an email of type ${purpose}`
|
throw `Unable to build an email of type ${purpose}`
|
||||||
|
@ -37,7 +33,7 @@ exports.buildEmail = async (email, user, purpose) => {
|
||||||
const context = {
|
const context = {
|
||||||
...(await getSettingsTemplateContext()),
|
...(await getSettingsTemplateContext()),
|
||||||
email,
|
email,
|
||||||
user,
|
user: user || {},
|
||||||
}
|
}
|
||||||
|
|
||||||
body = await processString(body, context)
|
body = await processString(body, context)
|
||||||
|
@ -51,14 +47,24 @@ exports.buildEmail = async (email, user, purpose) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.sendEmail = async ctx => {
|
exports.sendEmail = async ctx => {
|
||||||
const { groupId, email, purpose } = ctx.request.body
|
const { groupId, email, userId, purpose } = ctx.request.body
|
||||||
const db = new CouchDB(GLOBAL_DB)
|
const db = new CouchDB(GLOBAL_DB)
|
||||||
const params = {}
|
const params = {}
|
||||||
if (groupId) {
|
if (groupId) {
|
||||||
params.group = groupId
|
params.group = groupId
|
||||||
}
|
}
|
||||||
params.type = Configs.SMTP
|
params.type = Configs.SMTP
|
||||||
|
let user = {}
|
||||||
|
if (userId) {
|
||||||
|
user = db.get(userId)
|
||||||
|
}
|
||||||
const config = await determineScopedConfig(db, params)
|
const config = await determineScopedConfig(db, params)
|
||||||
const transport = createSMTPTransport(config)
|
const transport = createSMTPTransport(config)
|
||||||
|
const message = {
|
||||||
|
from: config.from,
|
||||||
|
subject: config.subject,
|
||||||
|
to: email,
|
||||||
|
html: await buildEmail(purpose, email, user),
|
||||||
|
}
|
||||||
|
await transport.sendMail(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,9 @@ function smtpValidation() {
|
||||||
return Joi.object({
|
return Joi.object({
|
||||||
port: Joi.number().required(),
|
port: Joi.number().required(),
|
||||||
host: Joi.string().required(),
|
host: Joi.string().required(),
|
||||||
from: Joi.string().email().required(),
|
from: Joi.string()
|
||||||
|
.email()
|
||||||
|
.required(),
|
||||||
secure: Joi.boolean().optional(),
|
secure: Joi.boolean().optional(),
|
||||||
selfSigned: Joi.boolean().optional(),
|
selfSigned: Joi.boolean().optional(),
|
||||||
auth: Joi.object({
|
auth: Joi.object({
|
||||||
|
|
|
@ -15,7 +15,10 @@ function buildEmailSendValidation() {
|
||||||
}).required().unknown(true))
|
}).required().unknown(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
router
|
router.post(
|
||||||
.post("/api/admin/email/send", buildEmailSendValidation(), controller.sendEmail)
|
"/api/admin/email/send",
|
||||||
|
buildEmailSendValidation(),
|
||||||
|
controller.sendEmail
|
||||||
|
)
|
||||||
|
|
||||||
module.exports = router
|
module.exports = router
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
const nodemailer = require("nodemailer")
|
||||||
|
|
||||||
|
exports.createSMTPTransport = (config) => {
|
||||||
|
const options = {
|
||||||
|
port: config.port,
|
||||||
|
host: config.host,
|
||||||
|
secure: config.secure || false,
|
||||||
|
auth: config.auth,
|
||||||
|
}
|
||||||
|
if (config.selfSigned) {
|
||||||
|
options.tls = {
|
||||||
|
rejectUnauthorized: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nodemailer.createTransport(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.verifyConfig = async config => {
|
||||||
|
const transport = exports.createSMTPTransport(config)
|
||||||
|
await transport.verify()
|
||||||
|
}
|
Loading…
Reference in New Issue