1
0
Fork 0
mirror of synced 2024-09-28 15:21:28 +12:00

prevent SMTP fallback for automations

This commit is contained in:
Martin McKeaveney 2021-09-27 16:28:39 +01:00
parent 0b4d6d6052
commit 30f60d9bae
4 changed files with 22 additions and 10 deletions

View file

@ -53,7 +53,7 @@ exports.run = async function ({ inputs }) {
contents = "<h1>No content</h1>"
}
try {
let response = await sendSmtpEmail(to, from, subject, contents)
let response = await sendSmtpEmail(to, from, subject, contents, true)
return {
success: true,
response,

View file

@ -34,7 +34,7 @@ function request(ctx, request) {
exports.request = request
// have to pass in the tenant ID as this could be coming from an automation
exports.sendSmtpEmail = async (to, from, subject, contents) => {
exports.sendSmtpEmail = async (to, from, subject, contents, automation) => {
// tenant ID will be set in header
const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + `/api/global/email/send`),
@ -46,6 +46,7 @@ exports.sendSmtpEmail = async (to, from, subject, contents) => {
contents,
subject,
purpose: "custom",
automation,
},
})
)

View file

@ -2,8 +2,16 @@ const { sendEmail } = require("../../../utilities/email")
const { getGlobalDB } = require("@budibase/auth/tenancy")
exports.sendEmail = async ctx => {
let { workspaceId, email, userId, purpose, contents, from, subject } =
ctx.request.body
let {
workspaceId,
email,
userId,
purpose,
contents,
from,
subject,
automation,
} = ctx.request.body
let user
if (userId) {
const db = getGlobalDB()
@ -15,6 +23,7 @@ exports.sendEmail = async ctx => {
contents,
from,
subject,
automation,
})
ctx.body = {
...response,

View file

@ -102,9 +102,10 @@ async function buildEmail(purpose, email, context, { user, contents } = {}) {
* Utility function for finding most valid SMTP configuration.
* @param {object} db The CouchDB database which is to be looked up within.
* @param {string|null} workspaceId If using finer grain control of configs a workspace can be used.
* @param {boolean|null} automation Whether or not the configuration is being fetched for an email automation.
* @return {Promise<object|null>} returns the SMTP configuration if it exists
*/
async function getSmtpConfiguration(db, workspaceId = null) {
async function getSmtpConfiguration(db, workspaceId = null, automation) {
const params = {
type: Configs.SMTP,
}
@ -116,10 +117,10 @@ async function getSmtpConfiguration(db, workspaceId = null) {
if (customConfig) {
return customConfig
}
}
// Use an SMTP fallback configuration from env variables
if (env.SMTP_FALLBACK_ENABLED) {
if (!automation && env.SMTP_FALLBACK_ENABLED) {
return {
port: env.SMTP_PORT,
host: env.SMTP_HOST,
@ -157,16 +158,17 @@ exports.isEmailConfigured = async (workspaceId = null) => {
* @param {string|undefined} contents If sending a custom email then can supply contents which will be added to it.
* @param {string|undefined} subject A custom subject can be specified if the config one is not desired.
* @param {object|undefined} info Pass in a structure of information to be stored alongside the invitation.
* @param {boolean|undefined} disableFallback Prevent email being sent from SMTP fallback to avoid spam.
* @return {Promise<object>} returns details about the attempt to send email, e.g. if it is successful; based on
* nodemailer response.
*/
exports.sendEmail = async (
email,
purpose,
{ workspaceId, user, from, contents, subject, info } = {}
{ workspaceId, user, from, contents, subject, info, automation } = {}
) => {
const db = getGlobalDB()
let config = (await getSmtpConfiguration(db, workspaceId)) || {}
let config = (await getSmtpConfiguration(db, workspaceId, automation)) || {}
if (Object.keys(config).length === 0 && !TEST_MODE) {
throw "Unable to find SMTP configuration."
}