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>" contents = "<h1>No content</h1>"
} }
try { try {
let response = await sendSmtpEmail(to, from, subject, contents) let response = await sendSmtpEmail(to, from, subject, contents, true)
return { return {
success: true, success: true,
response, response,

View File

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

View File

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

View File

@ -102,9 +102,10 @@ async function buildEmail(purpose, email, context, { user, contents } = {}) {
* Utility function for finding most valid SMTP configuration. * Utility function for finding most valid SMTP configuration.
* @param {object} db The CouchDB database which is to be looked up within. * @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 {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 * @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 = { const params = {
type: Configs.SMTP, type: Configs.SMTP,
} }
@ -119,7 +120,7 @@ async function getSmtpConfiguration(db, workspaceId = null) {
} }
// Use an SMTP fallback configuration from env variables // Use an SMTP fallback configuration from env variables
if (env.SMTP_FALLBACK_ENABLED) { if (!automation && env.SMTP_FALLBACK_ENABLED) {
return { return {
port: env.SMTP_PORT, port: env.SMTP_PORT,
host: env.SMTP_HOST, 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} 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 {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 {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 * @return {Promise<object>} returns details about the attempt to send email, e.g. if it is successful; based on
* nodemailer response. * nodemailer response.
*/ */
exports.sendEmail = async ( exports.sendEmail = async (
email, email,
purpose, purpose,
{ workspaceId, user, from, contents, subject, info } = {} { workspaceId, user, from, contents, subject, info, automation } = {}
) => { ) => {
const db = getGlobalDB() const db = getGlobalDB()
let config = (await getSmtpConfiguration(db, workspaceId)) || {} let config = (await getSmtpConfiguration(db, workspaceId, automation)) || {}
if (Object.keys(config).length === 0 && !TEST_MODE) { if (Object.keys(config).length === 0 && !TEST_MODE) {
throw "Unable to find SMTP configuration." throw "Unable to find SMTP configuration."
} }