Updating config management for SMTP as well as finalising the work around generating and sending emails.
This commit is contained in:
parent
fcd57cfcb5
commit
61ed6cf2bc
|
@ -5,6 +5,8 @@ const {
|
|||
getConfigParams,
|
||||
determineScopedConfig,
|
||||
} = require("@budibase/auth").db
|
||||
const { Configs } = require("../../../constants")
|
||||
const email = require("../../../utilities/email")
|
||||
|
||||
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 {
|
||||
const response = await db.post(configDoc)
|
||||
ctx.body = {
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
const CouchDB = require("../../../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 { getSettingsTemplateContext } = require("../../../utilities/templates")
|
||||
const { processString } = require("@budibase/string-templates")
|
||||
const nodemailer = require("nodemailer")
|
||||
const { createSMTPTransport } = require("../../../utilities/email")
|
||||
|
||||
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
||||
const TYPE = TemplateTypes.EMAIL
|
||||
|
@ -14,15 +18,7 @@ const FULL_EMAIL_PURPOSES = [
|
|||
EmailTemplatePurpose.PASSWORD_RECOVERY,
|
||||
]
|
||||
|
||||
function createSMTPTransport(config) {
|
||||
const transport = nodemailer.createTransport({
|
||||
port: config.port,
|
||||
host: config.host,
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
exports.buildEmail = async (email, user, purpose) => {
|
||||
async function buildEmail(purpose, email, user) {
|
||||
// this isn't a full email
|
||||
if (FULL_EMAIL_PURPOSES.indexOf(purpose) === -1) {
|
||||
throw `Unable to build an email of type ${purpose}`
|
||||
|
@ -37,7 +33,7 @@ exports.buildEmail = async (email, user, purpose) => {
|
|||
const context = {
|
||||
...(await getSettingsTemplateContext()),
|
||||
email,
|
||||
user,
|
||||
user: user || {},
|
||||
}
|
||||
|
||||
body = await processString(body, context)
|
||||
|
@ -51,14 +47,24 @@ exports.buildEmail = async (email, user, purpose) => {
|
|||
}
|
||||
|
||||
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 params = {}
|
||||
if (groupId) {
|
||||
params.group = groupId
|
||||
}
|
||||
params.type = Configs.SMTP
|
||||
let user = {}
|
||||
if (userId) {
|
||||
user = db.get(userId)
|
||||
}
|
||||
const config = await determineScopedConfig(db, params)
|
||||
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({
|
||||
port: Joi.number().required(),
|
||||
host: Joi.string().required(),
|
||||
from: Joi.string().email().required(),
|
||||
from: Joi.string()
|
||||
.email()
|
||||
.required(),
|
||||
secure: Joi.boolean().optional(),
|
||||
selfSigned: Joi.boolean().optional(),
|
||||
auth: Joi.object({
|
||||
|
|
|
@ -15,7 +15,10 @@ function buildEmailSendValidation() {
|
|||
}).required().unknown(true))
|
||||
}
|
||||
|
||||
router
|
||||
.post("/api/admin/email/send", buildEmailSendValidation(), controller.sendEmail)
|
||||
router.post(
|
||||
"/api/admin/email/send",
|
||||
buildEmailSendValidation(),
|
||||
controller.sendEmail
|
||||
)
|
||||
|
||||
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